Created
January 9, 2019 16:13
-
-
Save smitsgit/d0eda9bb100ff13fecf820c4085a871c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun generator() = generate<Int, String> { | |
var c = 1 | |
while (true) { | |
val op = yield(c) | |
when (op) { | |
"inc" -> c += 1 | |
"mult" -> c *= 2 | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
val g = generator() | |
val a = g.next("") // start | |
val b = g.next("inc") | |
val c = g.next("mult") | |
val d = g.next("inc") | |
println("$a $b $c $d") // 1, 2, 4, 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment