Created
May 25, 2012 10:08
-
-
Save robcd/2787106 to your computer and use it in GitHub Desktop.
Variation on sample program of SftI Ex 22.5, for future reference!
This file contains hidden or 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
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