diff options
author | Martin Odersky <odersky@gmail.com> | 2006-07-24 14:22:00 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2006-07-24 14:22:00 +0000 |
commit | 5bdedbd45315a010ad852b5f2b12715a824fee26 (patch) | |
tree | fd075aaa156ca6951799fac224fba231b45197c8 /src/actors | |
parent | 343c15fe833dc763750f8ed3ba65e9458da09d05 (diff) | |
download | scala-5bdedbd45315a010ad852b5f2b12715a824fee26.tar.gz scala-5bdedbd45315a010ad852b5f2b12715a824fee26.tar.bz2 scala-5bdedbd45315a010ad852b5f2b12715a824fee26.zip |
Diffstat (limited to 'src/actors')
-rwxr-xr-x | src/actors/scala/Reactions.scala | 54 | ||||
-rw-r--r-- | src/actors/scala/actors/multi/TimerThread.scala | 5 |
2 files changed, 55 insertions, 4 deletions
diff --git a/src/actors/scala/Reactions.scala b/src/actors/scala/Reactions.scala new file mode 100755 index 0000000000..041cc793bd --- /dev/null +++ b/src/actors/scala/Reactions.scala @@ -0,0 +1,54 @@ +package scala + +/** An enclosing trait for reactions. + * Examples of reactions are: actor.receive's, event.handle's, etc + * @param m The input type of a Reaction; typically the type of messages or events. + */ +trait Reactions[m] { + + /** The partial function underlying a reaction. Note that this is formulated + * in CPS style. + */ + type Re[r] = PartialFunction[m, (r => unit) => unit] + + /** Activate the given partial function `f', for instance by reading + * a message or waiting for an event, and applying `f' to the result. + */ + def activate[r](f: Re[r]): (r => unit) => unit + + /** The class of reactions + * @param r The type of values returned by the reaction. + * (More precisely, the type of values passed to its continuation) + * @param fun The partial function underlying a reaction + */ + class Reaction[+r](private val fun: Re[r]) extends Responder[r] { + + def respond(k: r => unit): unit = activate(fun)(k) + + override def map[s](f: r => s) = new Reaction[s] ( + fun andThen { + result: ((r => unit) => unit) => + k: (s => unit) => result((x: r) => k(f(x))) + } + ) + + def flatMap[s](f: r => Reaction[s]) = new Reaction[s] ( + fun andThen { + result: ((r => unit) => unit) => + k: (s => unit) => result((x: r) => f(x).respond(k)) + } + ) + + override def filter(p: r => boolean) = new Reaction[r] ( + fun andThen { + result: ((r => unit) => unit) => + k: (r => unit) => result((x: r) => if (p(x)) k(x) else ()) + } + ) + + def orElse[r1 >: r](that: Reaction[r1]) = new Reaction[r1] ( + this.fun orElse that.fun + ) + } +} + diff --git a/src/actors/scala/actors/multi/TimerThread.scala b/src/actors/scala/actors/multi/TimerThread.scala index fc1c2162da..436b27354f 100644 --- a/src/actors/scala/actors/multi/TimerThread.scala +++ b/src/actors/scala/actors/multi/TimerThread.scala @@ -25,10 +25,7 @@ case class Signal() object TimerThread extends AnyRef with Runnable { case class WakedActor(actor: MailBox, time: long, reason: String) extends Ordered[WakedActor] { var valid = true - def compare [b >: WakedActor <% Ordered[b]](that: b): int = that match { - case that2: WakedActor => -(this.time compare that2.time) - case _ => error("not comparable") - } + def compare (that: WakedActor): int = -(this.time compare that.time) } var queue = new PriorityQueue[WakedActor] |