Kotlin is to Java what ES6 is to old style JavaScript. A lot of people are stuck with JavaScript engine (like browser) or a JVM (like Android). Their native languages are atrocious, but using a completely non-native language leads to huge complexity ...
taw.hashnode.dev5 min readI totally agree about dropping Java.
I think you missed out on some of the terseness of Kotlin in your Fibonacci routine. You can use two techniques to get there:
fun fib(n: Int): Int = if (n < 3) 1 else fib(n - 1) + fib(n - 2)
fun main() {
IntRange(1, 30).forEach { println(fib(it)) }
fibonacci().take(30).forEach { println(it) }
}
fun fibonacci(): Sequence<Int> {
return generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first }
}
In the first, I've just cleaned up the fib() routine so that it looks how you'd actually write it in Kotlin. It really does look like a function now. To invoke it, I replaced the for loop with an IntRange and the used the forEach() method. That's a bit more Kotlin-like, IMHO.
The second I just lifted right from the Docs for generateSequence() which use Fibonacci as an example. It feels like the most Kotlin-like approach to the problem, treating the Fibonacci sequence like a sequence and then relying on the lazy loading to calculate each new value as/when/if required.
Maxi Contieri
Software Engineer
Wow !
What a series !
I am looking forward for next chapters !!!!