Skip to content

Instantly share code, notes, and snippets.

@lotharschulz
Last active October 23, 2022 18:53
Show Gist options
  • Save lotharschulz/79d3641c73f7b28120d1205ceef877fe to your computer and use it in GitHub Desktop.
Save lotharschulz/79d3641c73f7b28120d1205ceef877fe to your computer and use it in GitHub Desktop.

Kotlin fibonacci script

A kotlin script producing a fibunacci range inspired by clovisai's contribution to kotlin discussion: Project Loom will make coroutines obsolete.

val fibonacci = sequence {
    yield(1)
    var cur = 1
    var next = 1
    while (true) {
        yield(next)
        val tmp = cur + next
        cur = next
        next = tmp
    }
}

println(fibonacci.take(15).joinToString())

(suggested file name: kotlin-script-fibonacci.kts)

run script

kotlinc kotlin-script-fibonacci.kts -script -language-version 1.8 -include-runtime

Caution: kotlin custom scripting is experimental as of 1.7.20 (20221023).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment