3 ms·
I translated your example to Kotlin just for fun, as close as possible. Probably not idiomatic Kotlin, but pretty close, if you ask me. typealias FibPair =
by vbezhenar 8y ago
I translated your example to Kotlin just for fun, as close as possible. Probably not idiomatic Kotlin, but pretty close, if you ask me.
typealias FibPair = Pair<Int, Int>
val fibSeed = FibPair(0, 1)
fun fibNext(pv: FibPair) = pv.let { (p, v) -> FibPair(v, v + p) }
fun fibList(): Sequence<FibPair> = generateSequence(fibSeed, ::fibNext)
fun main() {
fibList().take(10).forEach { (p, _) -> print("$p ") }
}
Honestly I like Kotlin's Sequence abstraction much more than Java streams. It's extremely simple to implement and understand contrary to streams.