Kotlin
Beginner
1 min read
Sequences for Lazy Evaluation
Example
fun main() {
val numbers = (1..1_000_000).toList()
// Eager — creates two intermediate lists
val eagerResult = numbers
.filter { it % 2 == 0 }
.map { it * it }
.take(5)
println("Eager: $eagerResult")
// Lazy — no intermediate lists; stops after 5 elements found
val lazyResult = numbers.asSequence()
.filter { it % 2 == 0 }
.map { it * it }
.take(5)
.toList()
println("Lazy: $lazyResult")
// generateSequence — infinite Fibonacci sequence
val fibonacci: Sequence<Long> = generateSequence(Pair(0L, 1L)) {
Pair(it.second, it.first + it.second)
}.map { it.first }
println(fibonacci.take(10).toList())
// [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
// sequence builder with yield
val evens = sequence {
var n = 0
while (true) {
yield(n)
n += 2
}
}
println(evens.take(6).toList()) // [0, 2, 4, 6, 8, 10]
// Sequence of file lines (hypothetical large file)
val words = sequenceOf("the", "quick", "brown", "fox", "jumps")
val result = words
.filter { it.length >= 4 }
.map { it.replaceFirstChar { c -> c.uppercase() } }
.toList()
println(result) // [Quick, Brown, Jumps]
}