Kotlin 2 · Collections and lambdas
Process lazily
Sequences compute items on demand, which avoids intermediate lists for large pipelines.
val firstSquares = generateSequence(1) { it + 1 }
.map { it * it }
.filter { it % 2 == 1 }
.take(3)
.toList()
Official Kotlin 2 documentation