Created
March 18, 2012 13:37
-
-
Save alphaneet/2072574 to your computer and use it in GitHub Desktop.
scala.swing の PartialFunction の合成っぽいのを Actor でやってみた
This file contains 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
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