---
title: "Comment by Dave Barrett on \"100 Languages Speedrun: Episode 05: Kotlin\" | Hashnode"
description: "I 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 ther"
url: "https://hashnode.com/posts/100-languages-speedrun-episode-05-kotlin/619e9302e0f0d8286e67baee/comment/61a0fe1a9829fb749bcc0735"
---

[Discussion](https://hashnode.com/posts/100-languages-speedrun-episode-05-kotlin/619e9302e0f0d8286e67baee)

Comment [100 Languages Speedrun: Episode 05: Kotlin](https://hashnode.com/posts/100-languages-speedrun-episode-05-kotlin/619e9302e0f0d8286e67baee)

[Dave Barrett](https://hashnode.com/@PragmaticCoding)

[Nov 26, 2021](https://hashnode.com/posts/100-languages-speedrun-episode-05-kotlin/619e9302e0f0d8286e67baee/comment/61a0fe1a9829fb749bcc0735)

I 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.

[Tomasz Węgrzanowski](https://hashnode.com/@taw)

[Nov 26, 2021](https://hashnode.com/posts/100-languages-speedrun-episode-05-kotlin/619e9302e0f0d8286e67baee/comment/61a0fe1a9829fb749bcc0735#reply-61a125ecf0cb2e37690c55f8)

I'm trying to balance showcasing the language with keeping the posts approachable. Your single expression `fib` makes sense for an introductory post, but the generator sequence would need a lot of explaining, as few languages have something like that.
