Skip to content

Instantly share code, notes, and snippets.

@smitsgit
Created January 9, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smitsgit/d0eda9bb100ff13fecf820c4085a871c to your computer and use it in GitHub Desktop.
Save smitsgit/d0eda9bb100ff13fecf820c4085a871c to your computer and use it in GitHub Desktop.
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