Skip to content

Instantly share code, notes, and snippets.

@takscape
Created October 31, 2014 19:59
Show Gist options
  • Save takscape/a23391aec1b7e4a31863 to your computer and use it in GitHub Desktop.
Save takscape/a23391aec1b7e4a31863 to your computer and use it in GitHub Desktop.
Fibonacci Sequence in Kotlin (5)
import java.math.BigInteger
import kotlin.math.plus
fun main(args: Array<String>) {
fibo().take(10).forEach { println(it.first) }
}
private fun fibo() =
stream(BigInteger.ZERO to BigInteger.ONE, {(e) ->
e.second to (e.first plus e.second)
})
@vkopichenko
Copy link

Here is rewrite in current Kotlin:

    val fib = generateSequence(1 to 1) { it.second to it.first + it.second }.map { it.first }
    println(fib.take(10).joinToString())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment