(no title)
rbehrends | 1 year ago
Am I missing something here?
$ cat fib.kts
fun fib() = sequence {
var a = 1; var b = 1
while (true) {
yield(a)
a = b.also { b += a }
}
}
println(fib().take(10).joinToString(" -> "))
println(fib().first { it >= 50 })
$ kotlin fib.kts
1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13 -> 21 -> 34 -> 55
55
Of course, yield() is a function in Kotlin, not a keyword, but the same functionality is there.
yen223|1 year ago
rbehrends|1 year ago
The actual functionality is then provided by the coroutine system, though a lot of the heavy lifting is done by the optimizer to eliminate all or most of the runtime overhead.
poikroequ|1 year ago
Thank you for providing an example because I was a little confused by a couple other comments mentioning yield...