package com.mykotlin

import kotlin.coroutines.experimental.buildSequence

fun main(args: Array<String>) {
    val seq = buildSequence {
        for (i in 1..5) {
            // yield a square of i
            yield(i * i)
        }
        // yield a range
        yieldAll(26..28)
    }
    // print the sequence
    println(seq.map { "map -> $it" }.toList())

    val lazySeq = buildSequence {
        print("START ")
        for (i in 1..5) {
            yield(i)
            print("STEP ")
        }
        print("END")
    }
    // Print the first three elements of the sequence
    lazySeq.take(3).forEach { print("$it ") }
    println()

    println(testFibonacci(8))
}

fun testFibonacci(i: Int): List<Int> {
    val fibonacciSeq = buildSequence {
        var a = 0
        var b = 1
        yield(1)

        while (true) {
            yield(a + b)

            val tmp = a + b
            a = b
            b = tmp
        }
    }
    return fibonacciSeq.take(i).toList()
}


