I am trying to find the first two elements in a list that matches a condition (filtering), for this purpose I implemented the following code in kotlin:
val arr = 0 until 20 val res = arr.filter { i -> println("Filter: $i") i % 2 == 0 }.take(2)
Everything was fine until I realized that it filters the entire list, regardless of whether two items were found.
Using the Java 8 stream api stream, it works as expected.
val res2 = arr.toList().stream() .filter { i -> println("Filter: $i") i % 2 == 0 }.limit(2)
So, my questions, if they can be achieved using only the functions of Kotlin.
I know that I can use a simple loop, but I want to use aproach for functional programming.
source share