Skip to content

Instantly share code, notes, and snippets.

@raymondtay
Created April 3, 2012 03:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raymondtay/2289042 to your computer and use it in GitHub Desktop.
Save raymondtay/2289042 to your computer and use it in GitHub Desktop.
Interactive Input
import scala.util.continuations._
object AskMe {
def sleep(delay: Long) = shift{ k: (Unit => Int) =>
val runnable = new Runnable {
var leave = false
def run = {
while( ! leave ) {
Thread.sleep(delay)
val res = k()
res match {
case 0 => leave = true
case 1 => leave = false
}
}
}
}
val thread = new Thread(runnable)
thread.start
} // end of 'shift'
def main(args: Array[String]) = {
println("Before reset")
reset {
println("Before sleep")
sleep(2000)
println("Enter 1 to continue or 0 to exit")
val x = readInt
x match {
case 0 => { println("You've opted to go, well bye bye for now"); 0 }
case 1 => { println("You want to go on, well here goes"); 1 }
}
} // end of 'reset'
println("After reset")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment