Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Created May 1, 2011 20:50
Show Gist options
  • Save dhinojosa/950863 to your computer and use it in GitHub Desktop.
Save dhinojosa/950863 to your computer and use it in GitHub Desktop.
Thread Koan
import actors.Actor._
val guessNumber = actor {
loop {
react {
case (i: Int, caller: Actor) if (i > 64) => caller ! "Too High"
case (i: Int, caller: Actor) if (i < 64) => caller ! "Too Low"
case (i: Int, caller: Actor) if (i == 64) => {
caller ! "Ding"
exit('Successful)
}
}
}
}
val items = List(20, 23, "Boing", 90, 75, 70, 61, 64, "Boom", 65.34)
items.foreach {
x =>
guessNumber ! (x, self)
self.receiveWithin(100) {
case "Too High" => println("Too High")
case "Too Low" => println("Too Low")
case "Ding" => println("Just Right")
case _ => println("Timed Out")
}
}
Thread.sleep(1000) //Wait until all of it is completely done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment