From 8d3d085f4bc64bfe019e49675e64073e5a73f60a Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Mon, 2 Oct 2006 15:31:37 +0000 Subject: Clean-ups and new example for usage of input ch... Clean-ups and new example for usage of input channels. --- docs/examples/actors/Input.scala | 19 +++++++++++++++++++ docs/examples/actors/Joins.scala | 19 ++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 docs/examples/actors/Input.scala (limited to 'docs/examples') diff --git a/docs/examples/actors/Input.scala b/docs/examples/actors/Input.scala new file mode 100644 index 0000000000..05e3c08958 --- /dev/null +++ b/docs/examples/actors/Input.scala @@ -0,0 +1,19 @@ +package examples.actors + +import scala.actors.Actor._ +import scala.actors.Channel + +object Input extends Application { + + var in = new Channel[Pair[int, int]] + + actor(in) { + in.receive { + case Pair(x, y) => reply(x + y) + } + } + + actor { + scala.Console.println(in !? Pair(40, 2)) + } +} diff --git a/docs/examples/actors/Joins.scala b/docs/examples/actors/Joins.scala index 7ea2a50e11..637cb5d092 100644 --- a/docs/examples/actors/Joins.scala +++ b/docs/examples/actors/Joins.scala @@ -11,29 +11,29 @@ abstract class Producer[T] extends Iterator[T] { def next: T = { setCurrent() val res = current.get - current = (coordinator !? Next()).asInstanceOf[Option[T]] + current = (coordinator !? Next).asInstanceOf[Option[T]] res } private var current: Option[T] = null - private def setCurrent() = if (current == null) current = (coordinator !? Next()).asInstanceOf[Option[T]] + private def setCurrent() = if (current == null) current = (coordinator !? Next).asInstanceOf[Option[T]] - private case class HasValue(value: T) - private case class Next - private case class Done - private case class Continue + private case class HasValue(value: T) + private case object Next + private case object Done + private case object Continue /** A thread-based coordinator */ private val coordinator = actor { while (true) { receive { - case Next() => + case Next => reply { receive { case HasValue(v) => reply() Some(v) - case Done() => + case Done => None } } @@ -43,7 +43,8 @@ abstract class Producer[T] extends Iterator[T] { actor { produceValues - coordinator !? Done() + coordinator !? Done + () } } -- cgit v1.2.3