Created
February 2, 2019 13:16
-
-
Save cbruegg/2bb1a66c6aa6e53a5c5dfc4320b1c4f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
println(fib().take(5).toList()) | |
} | |
fun fib() = sequence<Int> { | |
yield(1) | |
yield(1) | |
var prev = 1 | |
var prevPrev = 1 | |
while (true) { | |
val cur = prevPrev + prev | |
yield(cur) | |
prevPrev = prev | |
prev = cur | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment