Created
October 31, 2014 19:27
-
-
Save takscape/8147e068a554d3ad537b to your computer and use it in GitHub Desktop.
Fibonacci Sequence in Kotlin (2)
This file contains hidden or 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
| 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