summaryrefslogtreecommitdiff
path: root/docs/examples
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2007-07-09 20:14:56 +0000
committerPhilipp Haller <hallerp@gmail.com>2007-07-09 20:14:56 +0000
commitb0b847f1ebd3660cc9fe716717d0937a9caa4134 (patch)
treec4c54a87f7ab759950a9c781f30dfc104707dbf6 /docs/examples
parentb766a0baf330c46dbab05afc61364e04bb5ffb6c (diff)
downloadscala-b0b847f1ebd3660cc9fe716717d0937a9caa4134.tar.gz
scala-b0b847f1ebd3660cc9fe716717d0937a9caa4134.tar.bz2
scala-b0b847f1ebd3660cc9fe716717d0937a9caa4134.zip
Added actors example that uses explicit reply d...
Added actors example that uses explicit reply destinations.
Diffstat (limited to 'docs/examples')
-rw-r--r--docs/examples/actors/auction.scala1
-rw-r--r--docs/examples/actors/channels.scala4
-rw-r--r--docs/examples/actors/fringe.scala79
3 files changed, 82 insertions, 2 deletions
diff --git a/docs/examples/actors/auction.scala b/docs/examples/actors/auction.scala
index f229fae995..1ca538c962 100644
--- a/docs/examples/actors/auction.scala
+++ b/docs/examples/actors/auction.scala
@@ -2,6 +2,7 @@ package examples.actors
import java.util.Date
import scala.actors._
+import scala.actors.Actor._
/** A simple demonstrator program implementing an online auction service
* The example uses the actor abstraction defined in the API of
diff --git a/docs/examples/actors/channels.scala b/docs/examples/actors/channels.scala
index 498fb9602c..9267882e50 100644
--- a/docs/examples/actors/channels.scala
+++ b/docs/examples/actors/channels.scala
@@ -13,8 +13,8 @@ object channels extends Application {
b ! Msg(Ch1, Ch2)
react {
- case Ch1 ! x => Console.println("received on int channel: "+x)
- case Ch2 ! y => Console.println("received on String channel: "+y)
+ case x => Console.println("received on int channel: "+x)
+ //case y => Console.println("received on String channel: "+y)
}
}
diff --git a/docs/examples/actors/fringe.scala b/docs/examples/actors/fringe.scala
new file mode 100644
index 0000000000..96edd396e9
--- /dev/null
+++ b/docs/examples/actors/fringe.scala
@@ -0,0 +1,79 @@
+package examples.actors
+
+import scala.actors.Actor._
+import scala.actors.{Channel, OutputChannel}
+
+/**
+ @author Philipp Haller
+ @version 1.0, 07/09/2007
+ */
+object fringe extends Application {
+
+ abstract class Tree
+ case class Node(left: Tree, right: Tree) extends Tree
+ case class Leaf(v: Int) extends Tree
+
+ val comparator = actor {
+ val extractor1 = actor(extractorBehavior())
+ val extractor2 = actor(extractorBehavior())
+ val ch1 = new Channel[Any]
+ val ch2 = new Channel[Any]
+ loop {
+ react {
+ case ('Fringe, tree1, tree2) =>
+ extractor1.send(('Fringe, tree1), ch1)
+ extractor2.send(('Fringe, tree2), ch2)
+ self ! Triple('Equal, ch1.?, ch2.?)
+
+ case ('Equal, atom1, atom2) =>
+ println("comparing "+atom1+" and "+atom2)
+ if (atom1 == atom2) atom1 match {
+ case None =>
+ println("same fringe")
+ exit()
+ case _ =>
+ self ! Triple('Equal, ch1.?, ch2.?)
+ } else {
+ println("fringes differ")
+ exit()
+ }
+ }
+ }
+ }
+
+ val extractorBehavior = () => {
+ var output: OutputChannel[Any] = null
+ loop {
+ react {
+ case ('Fringe, tree) =>
+ output = sender
+ self ! ('Extract, tree)
+
+ case ('Extract, tree) => tree match {
+ case atom @ Leaf(_) =>
+ println("sending "+Some(atom))
+ output ! Some(atom)
+ sender ! 'Continue
+
+ case Node(left, right) =>
+ val outer = self
+ val outerCont = sender
+ val cont = actor {
+ react {
+ case 'Continue =>
+ outer.send(('Extract, right), outerCont)
+ }
+ }
+ self.send(('Extract, left), cont)
+ }
+
+ case 'Continue =>
+ output ! None
+ exit()
+ }
+ }
+ }
+
+ comparator ! ('Fringe, Node(Leaf(5), Node(Leaf(7), Leaf(3))),
+ Node(Leaf(5), Node(Leaf(7), Leaf(3))))
+}