takeWhile

The takeWhile function accepts a lambda expression. This function keeps fetching elements until the condition is not satisfied:

var numbers = listOf<Int>(1,2,3,4,5,4,3,8,9,10)
newList = numbers.takeWhile { it < 5 }
println("Take all elements from the beginning of the list, until the element is 5 or more").
println(newList)

Here, the output is [1, 2, 3, 4]. Notice that the numbers list contains a total of six elements that are less than five, but the takeWhile function immediately stops executing when the condition becomes false

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8