Introduction to Delimited Continuations/Embedded shifts
The imperative-looking prompt
method can be used to cleanly embed many shift
blocks in the run
method, so we can have multiple-step asynchronous user workflows.
var c: String => Unit = _
def prompt() = shift { k: (String => Unit) => c = k }
def run() = reset {
println("What is your name? ** call c(<your name>) to continue **")
val name = prompt()
println("Hello, " + name + "!")
println("How old are you? ** call c(<your age>) to continue **")
val age = prompt()
println("You are " + age + " years old, " + name + "!")
println("Where do you live? ** call c(<your town>) to continue **")
val town = prompt()
println("You are " + age + " years old and live in " + town + ", " + name + "!")
}
|