January 2018
Intermediate to advanced
434 pages
14h 1m
English
We will be using the take function and its variants for limiting the items in the list.
take(n): Returns a list of the first n items:
fun main(args: Array<String>) { val list= listOf(1,2,3,4,5) val limitedList=list.take(3) println(limitedList)}//Output: [1,2,3]
takeLast(n): Returns a list containing the last [n] elements:
fun main(args: Array<String>) { val list= listOf(1,2,3,4,5) val limitedList=list.takeLast(3) println(limitedList)}//Output: [3,4,5]
takeWhile{ predicate }: Returns a list containing the first elements satisfying the given [predicate]:
val list= listOf(1,2,3,4,5)val limitedList=list.takeWhile { it<3 }println(limitedList)//Output: [1,2]
takeLastWhile{predicate}: Works a bit like takeWhile, except that it evaluates ...
Read now
Unlock full access