Skip to content

Instantly share code, notes, and snippets.

@robcd
Created May 25, 2012 10:08
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 robcd/2787106 to your computer and use it in GitHub Desktop.
Save robcd/2787106 to your computer and use it in GitHub Desktop.
Variation on sample program of SftI Ex 22.5, for future reference!
import scala.util.continuations._
object Main extends App {
val a = "Mary was a little lamb".split(" ")
type Next = (Boolean => String)
var next: Next = null
val firstResultOfShiftBlock = reset {
var i = 0
while (i < a.length) {
val whateverWasPassedToNext = shift { restOfComputationWhenShiftCalled: Next =>
val res = if (next == null) "This first result is returned by reset."
else a(i) // returned by next
next = restOfComputationWhenShiftCalled
res
}
if (whateverWasPassedToNext == true) i += 1
}
"empty"
}
println(next(false)) // Mary: don't increment first time round
println(next(true)) // was
println(next(true)) // a
println(next(false)) // a: didn't increment
println(next(true)) // little
println(next(true)) // lamb
println(next(true)) // empty: shift not encountered so get value of reset block
println(next(true)) // empty: next unchanged!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment