From ba31aaae833ee9caa33c5d7f713003f901b299eb Mon Sep 17 00:00:00 2001 From: michelou Date: Thu, 14 Feb 2008 16:43:18 +0000 Subject: updated code --- docs/examples/actors/auction.scala | 19 ++++++++++--------- docs/examples/actors/boundedbuffer.scala | 15 ++++++++------- docs/examples/actors/links.scala | 16 ++++++++-------- docs/examples/actors/looping.scala | 6 ++---- docs/examples/actors/message.scala | 14 ++++++++------ docs/examples/actors/pingpong.scala | 10 +++++----- docs/examples/actors/producers.scala | 32 +++++++++++++++++--------------- 7 files changed, 58 insertions(+), 54 deletions(-) (limited to 'docs') diff --git a/docs/examples/actors/auction.scala b/docs/examples/actors/auction.scala index 513e9a6ad0..5bda010c57 100644 --- a/docs/examples/actors/auction.scala +++ b/docs/examples/actors/auction.scala @@ -1,6 +1,7 @@ package examples.actors import java.util.Date + import scala.actors._ import scala.actors.Actor._ @@ -10,20 +11,20 @@ import scala.actors.Actor._ */ trait AuctionMessage -case class Offer(bid: int, client: Actor) extends AuctionMessage // make a bid +case class Offer(bid: Int, client: Actor) extends AuctionMessage // make a bid case class Inquire(client: Actor) extends AuctionMessage // inquire status trait AuctionReply -case class Status(asked: int, expiration: Date) // asked sum, expiration date +case class Status(asked: Int, expiration: Date) // asked sum, expiration date extends AuctionReply case object BestOffer extends AuctionReply // yours is the best offer -case class BeatenOffer(maxBid: int) extends AuctionReply // offer beaten by maxBid +case class BeatenOffer(maxBid: Int) extends AuctionReply // offer beaten by maxBid case class AuctionConcluded(seller: Actor, client: Actor) // auction concluded extends AuctionReply case object AuctionFailed extends AuctionReply // failed with no bids case object AuctionOver extends AuctionReply // bidding is closed -class AuctionActor(seller: Actor, minBid: int, closing: Date) extends Actor { +class AuctionActor(seller: Actor, minBid: Int, closing: Date) extends Actor { val timeToShutdown = 3000 // msec val bidIncrement = 10 @@ -68,7 +69,7 @@ class AuctionActor(seller: Actor, minBid: int, closing: Date) extends Actor { object auction { - val random = new java.util.Random() + val random = new Random val minBid = 100 val closing = new Date(new Date().getTime() + 4000) @@ -76,11 +77,11 @@ object auction { val seller = Actor.actor { } val auction = new AuctionActor(seller, minBid, closing) - def client(i: int, increment: int, top: int) = new Actor { + def client(i: Int, increment: Int, top: Int) = new Actor { val name = "Client " + i def log(msg: String) = Console.println(name + ": " + msg) - var max: int = _ - var current: int = 0 + var max: Int = _ + var current: Int = 0 def act() { log("started") auction ! Inquire(this) @@ -120,7 +121,7 @@ object auction { } } - def main(args: Array[String]) = { + def main(args: Array[String]) { seller.start() auction.start() client(1, 20, 200).start() diff --git a/docs/examples/actors/boundedbuffer.scala b/docs/examples/actors/boundedbuffer.scala index 2cef00abb8..d4ab9715c1 100644 --- a/docs/examples/actors/boundedbuffer.scala +++ b/docs/examples/actors/boundedbuffer.scala @@ -3,21 +3,22 @@ package examples.actors import scala.actors.Actor._ object boundedbuffer { - class BoundedBuffer[T](N: int) { + class BoundedBuffer[T](N: Int) { private case class Put(x: T) private case object Get private case object Stop private val buffer = actor { val buf = new Array[T](N) - var in = 0; var out = 0; var n = 0 + var in, out, n = 0 loop { react { case Put(x) if n < N => - buf(in) = x; in = (in + 1) % N; n = n + 1; reply() + buf(in) = x; in = (in + 1) % N; n += 1; reply() case Get if n > 0 => - val r = buf(out); out = (out + 1) % N; n = n - 1; reply(r) - case Stop => reply(); exit("stopped") + val r = buf(out); out = (out + 1) % N; n -= 1; reply(r) + case Stop => + reply(); exit("stopped") } } } @@ -27,10 +28,10 @@ object boundedbuffer { def stop() { buffer !? Stop } } - def main(args: Array[String]) = { + def main(args: Array[String]) { val buf = new BoundedBuffer[Int](1) buf.put(42) - scala.Console.println("" + buf.get) + println("" + buf.get) buf.stop() } } diff --git a/docs/examples/actors/links.scala b/docs/examples/actors/links.scala index 679cdf9659..7e31eb09d1 100644 --- a/docs/examples/actors/links.scala +++ b/docs/examples/actors/links.scala @@ -12,21 +12,21 @@ object links extends Application { start ! Stop } - def p(n: int): Actor = + def p(n: Int): Actor = if (n == 0) top1() else top(p(n-1), n) - def top(a: Actor, n: int): Actor = actor { - Console.println("starting actor " + n + " (" + Thread.currentThread() + ")") + def top(a: Actor, n: Int): Actor = actor { + println("starting actor " + n + " (" + Thread.currentThread() + ")") self.trapExit = true link(a) loop { receive { case ex @ Exit(from, reason) => - Console.println("Actor " + n + " received " + ex) + println("Actor " + n + " received " + ex) exit('finished) case any => { - Console.println("Actor " + n + " received " + any) + println("Actor " + n + " received " + any) a ! any } } @@ -34,13 +34,13 @@ object links extends Application { } def top1(): Actor = actor { - Console.println("starting last actor" + " (" + Thread.currentThread() + ")") + println("starting last actor" + " (" + Thread.currentThread() + ")") receive { case Stop => - Console.println("Last actor now exiting") + println("Last actor now exiting") exit('abnormal) case any => - Console.println("Last actor received " + any) + println("Last actor received " + any) top1() } } diff --git a/docs/examples/actors/looping.scala b/docs/examples/actors/looping.scala index 72adddcf4c..1ce2e2e5e8 100644 --- a/docs/examples/actors/looping.scala +++ b/docs/examples/actors/looping.scala @@ -10,7 +10,7 @@ object looping extends Application { loop { react { case A => - cnt = cnt + 1 + cnt += 1 if (cnt % 2 != 0) continue if (cnt < 10) println("received A") @@ -22,7 +22,5 @@ object looping extends Application { } } - for (val i <- 0 until 10) { - a ! A - } + for (i <- 0 until 10) a ! A } diff --git a/docs/examples/actors/message.scala b/docs/examples/actors/message.scala index 303dcbe9e1..9158fef695 100644 --- a/docs/examples/actors/message.scala +++ b/docs/examples/actors/message.scala @@ -1,10 +1,11 @@ package examples.actors -import scala.actors._; import scala.actors.Actor._ +import scala.actors._ +import scala.actors.Actor._ object message { - def main(args: Array[String]) = { - val n = try { Integer.parseInt(args(0)) } + def main(args: Array[String]) { + val n = try { args(0).toInt } catch { case _ => println("Usage: examples.actors.message ") @@ -14,9 +15,9 @@ object message { val finalSum = n * nActors Scheduler.impl = new SingleThreadedScheduler - def beh(next: Actor, sum: int): unit = + def beh(next: Actor, sum: Int) { react { - case value: int => + case value: Int => val j = value + 1; val nsum = sum + j if (next == null && nsum >= n * j) println(nsum) @@ -25,11 +26,12 @@ object message { if (nsum < n * j) beh(next, nsum) } } + } def actorChain(i: Int, a: Actor): Actor = if (i > 0) actorChain(i-1, actor(beh(a, 0))) else a val firstActor = actorChain(nActors, null) - var i = n; while (i > 0) { firstActor ! 0; i = i-1 } + var i = n; while (i > 0) { firstActor ! 0; i -= 1 } } } diff --git a/docs/examples/actors/pingpong.scala b/docs/examples/actors/pingpong.scala index 0818606ae6..4ed225c662 100644 --- a/docs/examples/actors/pingpong.scala +++ b/docs/examples/actors/pingpong.scala @@ -20,7 +20,7 @@ object pingpong extends Application { pong.start } -class Ping(count: int, pong: Actor) extends Actor { +class Ping(count: Int, pong: Actor) extends Actor { def act() { var pingsLeft = count - 1 pong ! Ping @@ -28,12 +28,12 @@ class Ping(count: int, pong: Actor) extends Actor { react { case Pong => if (pingsLeft % 1000 == 0) - Console.println("Ping: pong") + println("Ping: pong") if (pingsLeft > 0) { pong ! Ping pingsLeft -= 1 } else { - Console.println("Ping: stop") + println("Ping: stop") pong ! Stop exit() } @@ -49,11 +49,11 @@ class Pong extends Actor { react { case Ping => if (pongCount % 1000 == 0) - Console.println("Pong: ping "+pongCount) + println("Pong: ping "+pongCount) sender ! Pong pongCount += 1 case Stop => - Console.println("Pong: stop") + println("Pong: stop") exit() } } diff --git a/docs/examples/actors/producers.scala b/docs/examples/actors/producers.scala index 859de901e6..648d409233 100644 --- a/docs/examples/actors/producers.scala +++ b/docs/examples/actors/producers.scala @@ -14,11 +14,12 @@ abstract class Producer[T] { /** A signal to stop the coordinator. */ private val Stop = new Object - protected def produce(x: T): unit = { + protected def produce(x: T) { coordinator ! Some(x) receive { case Next => } } - protected def produceValues: unit + + protected def produceValues: Unit def iterator = new Iterator[T] { private var current: Any = Undefined @@ -27,7 +28,7 @@ abstract class Producer[T] { current } - def hasNext: boolean = lookAhead match { + def hasNext: Boolean = lookAhead match { case Some(x) => true case None => { coordinator ! Stop; false } } @@ -45,7 +46,8 @@ abstract class Producer[T] { reply { receive { case x: Option[_] => x } } - case Stop => exit('stop) + case Stop => + exit('stop) } } } @@ -61,13 +63,13 @@ abstract class Producer[T] { object producers extends Application { - class Tree(val left: Tree, val elem: int, val right: Tree) + 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] { + class PreOrder(n: Tree) extends Producer[Int] { def produceValues = traverse(n) def traverse(n: Tree) { if (n != null) { @@ -78,7 +80,7 @@ object producers extends Application { } } - class PostOrder(n: Tree) extends Producer[int] { + class PostOrder(n: Tree) extends Producer[Int] { def produceValues = traverse(n) def traverse(n: Tree) { if (n != null) { @@ -89,7 +91,7 @@ object producers extends Application { } } - class InOrder(n: Tree) extends Producer[int] { + class InOrder(n: Tree) extends Producer[Int] { def produceValues = traverse(n) def traverse(n: Tree) { if (n != null) { @@ -101,12 +103,12 @@ object producers extends Application { } actor { - Console.print("PreOrder:") - for (x <- new PreOrder(tree).iterator) Console.print(" "+x) - Console.print("\nPostOrder:") - for (x <- new PostOrder(tree).iterator) Console.print(" "+x) - Console.print("\nInOrder:") - for (x <- new InOrder(tree).iterator) Console.print(" "+x) - Console.print("\n") + 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") } } -- cgit v1.2.3