diff options
author | Philipp Haller <hallerp@gmail.com> | 2006-11-13 16:14:50 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2006-11-13 16:14:50 +0000 |
commit | 1a98bd7b476138c991c30c5bfa071af5fe539385 (patch) | |
tree | b543d98d9613c80df9b717ed60187b17335c06a9 /docs/examples | |
parent | bb30761427b8dffea0a1d455fd10e4e86447076a (diff) | |
download | scala-1a98bd7b476138c991c30c5bfa071af5fe539385.tar.gz scala-1a98bd7b476138c991c30c5bfa071af5fe539385.tar.bz2 scala-1a98bd7b476138c991c30c5bfa071af5fe539385.zip |
Cleaned-up new iterator example (automatic term...
Cleaned-up new iterator example (automatic termination etc).
Diffstat (limited to 'docs/examples')
-rw-r--r-- | docs/examples/actors/joins5.scala | 47 |
1 files changed, 16 insertions, 31 deletions
diff --git a/docs/examples/actors/joins5.scala b/docs/examples/actors/joins5.scala index 220ce2a131..44faa4ed53 100644 --- a/docs/examples/actors/joins5.scala +++ b/docs/examples/actors/joins5.scala @@ -1,4 +1,6 @@ -import scala.actors._ +package examples.actors + +import scala.actors.Actor import scala.actors.Actor._ abstract class Producer[T] { @@ -33,22 +35,19 @@ abstract class Producer[T] { } case object Stop - class StopException extends Throwable - /** A thread-based coordinator */ private val coordinator: Actor = actor { - try { - while (true) { + var continue = true + while (continue) { receive { case Next => producer ! Next reply { receive { case x: Option[_] => x } } - case Stop => throw new StopException + case Stop => continue = false } } - } catch { case _: StopException => } } private val producer: Actor = actor { @@ -60,20 +59,7 @@ abstract class Producer[T] { } } -object Test extends Application { - - def from(m: int, n: int) = new Producer[int] { - def produceValues = for (val i <- m until n) produce(i) - } - - // note that it works from the main thread - val it = from(1, 10).iterator - while (it.hasNext) { - Console.println(it.next) - } -} - -object Test2 extends Application { +object Joins 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) @@ -114,14 +100,13 @@ object Test2 extends Application { } } - Debug.level = 3 - - // note that it works from the main thread - Console.print("PreOrder:") - for (val x <- new PreOrder(tree).iterator) Console.print(" "+x) - Console.print("\nPostOrder:") - for (val x <- new PostOrder(tree).iterator) Console.print(" "+x) - Console.print("\nInOrder:") - for (val x <- new InOrder(tree).iterator) Console.print(" "+x) - Console.print("\n") + actor { + Console.print("PreOrder:") + for (val x <- new PreOrder(tree).iterator) Console.print(" "+x) + Console.print("\nPostOrder:") + for (val x <- new PostOrder(tree).iterator) Console.print(" "+x) + Console.print("\nInOrder:") + for (val x <- new InOrder(tree).iterator) Console.print(" "+x) + Console.print("\n") + } } |