Skip to content

Instantly share code, notes, and snippets.

@alphaneet
Created March 18, 2012 13:37
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 alphaneet/2072574 to your computer and use it in GitHub Desktop.
Save alphaneet/2072574 to your computer and use it in GitHub Desktop.
scala.swing の PartialFunction の合成っぽいのを Actor でやってみた
trait Reactions extends scala.actors.Actor {
type Reactor = PartialFunction[Any, Unit]
val reactions = scala.collection.mutable.Set[Reactor]()
override def exit() = super.exit()
def act() {
loop {
react {
reactions.reduceLeft { _ orElse _ }
}
}
}
}
case object KEY_INPUT
case object MOUSE_INPUT
object Console extends App {
val a = new Reactions {}
a.reactions += {
case KEY_INPUT =>
println("a: key input")
}
val b = new Reactions {}
b.reactions += {
case KEY_INPUT =>
println("b: key input")
}
b.reactions += {
case MOUSE_INPUT =>
println("b: mouse input")
}
a.start()
b.start()
a ! KEY_INPUT
b ! KEY_INPUT
a ! MOUSE_INPUT // a には MOUSE_INPUT がないので無視される
b ! MOUSE_INPUT
a.exit()
b.exit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment