diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-12-09 11:06:22 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-12-09 17:11:05 -0800 |
commit | e55510607031aaf7b8473453f83c44a26c9b97e0 (patch) | |
tree | 987d4aeeef387ed435acc64ffadfdf78e6f65bce /docs/examples/actors/producers.scala | |
parent | dc6dd58d9da441b591580684f3d2f38d7898e7d4 (diff) | |
download | scala-e55510607031aaf7b8473453f83c44a26c9b97e0.tar.gz scala-e55510607031aaf7b8473453f83c44a26c9b97e0.tar.bz2 scala-e55510607031aaf7b8473453f83c44a26c9b97e0.zip |
Remove docs/examples; they reside at scala/scala-dist
Diffstat (limited to 'docs/examples/actors/producers.scala')
-rw-r--r-- | docs/examples/actors/producers.scala | 114 |
1 files changed, 0 insertions, 114 deletions
diff --git a/docs/examples/actors/producers.scala b/docs/examples/actors/producers.scala deleted file mode 100644 index 80e5ae33d3..0000000000 --- a/docs/examples/actors/producers.scala +++ /dev/null @@ -1,114 +0,0 @@ -package examples.actors - -import scala.actors.Actor -import scala.actors.Actor._ - -abstract class Producer[T] { - - /** A signal that the next value should be produced. */ - private val Next = new Object - - /** A label for an undefined state of the iterators. */ - private val Undefined = new Object - - /** A signal to stop the coordinator. */ - private val Stop = new Object - - protected def produce(x: T) { - coordinator ! Some(x) - receive { case Next => } - } - - protected def produceValues: Unit - - def iterator = new Iterator[T] { - private var current: Any = Undefined - private def lookAhead = { - if (current == Undefined) current = coordinator !? Next - current - } - - def hasNext: Boolean = lookAhead match { - case Some(x) => true - case None => { coordinator ! Stop; false } - } - - def next: T = lookAhead match { - case Some(x) => current = Undefined; x.asInstanceOf[T] - } - } - - private val coordinator: Actor = actor { - loop { - react { - case Next => - producer ! Next - reply { - receive { case x: Option[_] => x } - } - case Stop => - exit('stop) - } - } - } - - private val producer: Actor = actor { - receive { - case Next => - produceValues - coordinator ! None - } - } -} - -object producers extends Application { - - class Tree(val left: Tree, val elem: Int, val right: Tree) - def node(left: Tree, elem: Int, right: Tree): Tree = new Tree(left, elem, right) - def node(elem: Int): Tree = node(null, elem, null) - - def tree = node(node(node(3), 4, node(6)), 8, node(node(9), 10, node(11))) - - class PreOrder(n: Tree) extends Producer[Int] { - def produceValues = traverse(n) - def traverse(n: Tree) { - if (n != null) { - produce(n.elem) - traverse(n.left) - traverse(n.right) - } - } - } - - class PostOrder(n: Tree) extends Producer[Int] { - def produceValues = traverse(n) - def traverse(n: Tree) { - if (n != null) { - traverse(n.left) - traverse(n.right) - produce(n.elem) - } - } - } - - class InOrder(n: Tree) extends Producer[Int] { - def produceValues = traverse(n) - def traverse(n: Tree) { - if (n != null) { - traverse(n.left) - produce(n.elem) - traverse(n.right) - } - } - } - - actor { - print("PreOrder:") - for (x <- new PreOrder(tree).iterator) print(" "+x) - print("\nPostOrder:") - for (x <- new PostOrder(tree).iterator) print(" "+x) - print("\nInOrder:") - for (x <- new InOrder(tree).iterator) print(" "+x) - print("\n") - } -} |