October 2018
Intermediate to advanced
370 pages
9h 15m
English
This function performs arithmetic operations on all the elements of a list and returns a result. To understand the concept of the fold function, we'll use addition as the operation. Let's add all the elements of the list and return a result. The fold function takes an integer parameter and a lambda expression. The first parameter indicates the initial value, and the second parameter takes a lambda expression for adding two values:
var numbers = listOf<Int>(1,2,3,4,5)var result = numbers.fold(0){i,j -> i + j}println("From beginning : add all elements of the list, Initial value is 0: " + result)
The fold function takes 0 as an initializer, adds all elements, and returns a result. To understand how things works under the hood, ...
Read now
Unlock full access