February 2018
Intermediate to advanced
350 pages
7h 35m
English
The take functions work in just the opposite way to the drop functions. You can take a selection from the collection and ignore the rest.
Have a look at the following program:
fun main(args: Array<String>) {
val list = 1.until(50).toList()
println("list.take(25) -> ${list.take(25)}")//(1)
println("list.takeLast(25) -> ${list.takeLast(25)}")//(2)
println("list.takeWhile { it<=10 } -> ${list.takeWhile { it<=10 }}")//(3)
println("list.takeLastWhile { it>=40 } -> ${list.takeLastWhile { it>=40 }}")//(4)
}
While statements on comment (1) and comment (2) are opposite to the drop functions earlier, they just take and print the 25 items from the list.
The statement on comment (3) is a bit different, here we used the takeWhile
Read now
Unlock full access