February 2018
Intermediate to advanced
350 pages
7h 35m
English
Think of a situation where you need to filter the items in a collection. For example, when you want to obtain only even numbers from a list of integers. The filter function is there to help you in these scenarios.
The filter function receives all the elements of the collection as each iteration and should return true or false, based on its determination of whether the passed item should be on the resultant list or not.
Go through the following program:
fun main(args: Array<String>) { val list = 1.until(50).toList()//(1) val filteredListEven = list.filter { it%2==0 }//(2) println("filteredListEven -> $filteredListEven") val filteredListPSquare = list.filter { val sqroot = sqrt(it.toDouble()).roundToInt() sqroot*sqroot==it ...Read now
Unlock full access