|
|
|
|
|
by rbehrends
654 days ago
|
|
> the 'yield' keyword 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. |
|