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.