Skip to content

Instantly share code, notes, and snippets.

@takscape
Created October 31, 2014 19:27
Show Gist options
  • Select an option

  • Save takscape/8147e068a554d3ad537b to your computer and use it in GitHub Desktop.

Select an option

Save takscape/8147e068a554d3ad537b to your computer and use it in GitHub Desktop.
Fibonacci Sequence in Kotlin (2)
import java.math.BigInteger
import kotlin.math.plus
fun main(args: Array<String>) {
fibo().take(10).forEach { println(it) }
}
data class FiboEntry(val cur : BigInteger, val next : BigInteger)
private fun fibo() : FunctionStream<BigInteger> {
var tmp = FiboEntry(BigInteger.ZERO, BigInteger.ONE)
return FunctionStream({() ->
val (cur, next) = tmp
tmp = tmp.copy(next, cur plus next)
cur
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment