From 5e87c33e2aaec7a0c9bcd6f257a9a8e4f9664529 Mon Sep 17 00:00:00 2001 From: michelou Date: Mon, 8 Dec 2003 16:17:42 +0000 Subject: - adapted to current Scala version --- sources/examples/ComputeServer.scala | 20 +++++++++++---- sources/examples/boundedbuffer.scala | 46 ++++++++++++++++++---------------- sources/examples/buffer1.scala | 48 +++++++++++++++++++++++++++++------- sources/examples/fors.scala | 6 +++-- sources/examples/iterators.scala | 11 +++++++-- sources/examples/maps.scala | 28 ++++++++++++++++++++- sources/examples/sort.scala | 4 ++- sources/examples/sort1.scala | 6 +++-- sources/examples/sort2.scala | 6 +++-- sources/examples/typeinf.scala | 23 +++++++++-------- 10 files changed, 143 insertions(+), 55 deletions(-) (limited to 'sources/examples') diff --git a/sources/examples/ComputeServer.scala b/sources/examples/ComputeServer.scala index c3ea4907ad..034b996498 100644 --- a/sources/examples/ComputeServer.scala +++ b/sources/examples/ComputeServer.scala @@ -3,30 +3,40 @@ package examples; import concurrent._, concurrent.ops._; class ComputeServer(n: Int) { + private trait Job { type t; def task: t; - def return(x: t): Unit; + def ret(x: t): Unit; } private val openJobs = new Channel[Job](); private def processor(i: Int): Unit = { - while (True) { + while (true) { val job = openJobs.read; - job.return(job.task) + Console.println("read a job"); + job.ret(job.task) } } + def future[a](def p: a): () => a = { val reply = new SyncVar[a](); openJobs.write{ new Job { type t = a; def task = p; - def return(x: a) = reply.set(x); + def ret(x: a) = reply.set(x); } } () => reply.get } - replicate(1,n){processor} + + spawn(replicate(0, n) { processor }) +} + +object Test with Executable { + val server = new ComputeServer(1); + val f = server.future(42); + Console.println(f()) } diff --git a/sources/examples/boundedbuffer.scala b/sources/examples/boundedbuffer.scala index 6f71f72cf5..fdc6feb9b4 100644 --- a/sources/examples/boundedbuffer.scala +++ b/sources/examples/boundedbuffer.scala @@ -1,30 +1,34 @@ package examples; -import concurrent.ops._; +object boundedbuffer { -class BoundedBuffer[a](N: Int) extends Monitor() { - var in = 0, out = 0, n = 0; - val elems = new Array[a](N); + import concurrent.ops._; - def put(x: a) = synchronized { - await (n < N); - elems(in) = x ; in = (in + 1) % N ; n = n + 1; - if (n == 1) notifyAll(); + class BoundedBuffer[a](N: Int) extends Monitor() { + var in = 0, out = 0, n = 0; + val elems = new Array[a](N); + + def put(x: a) = synchronized { + await (n < N); + elems(in) = x; in = (in + 1) % N; n = n + 1; + if (n == 1) notifyAll(); + } + + def get: a = synchronized { + await (n != 0); + val x = elems(out); out = (out + 1) % N ; n = n - 1; + if (n == N - 1) notifyAll(); + x + } } - def get: a = synchronized { - await (n != 0); - val x = elems(out) ; out = (out + 1) % N ; n = n - 1; - if (n == N - 1) notifyAll(); - x + def main(args: Array[String]) = { + val buf = new BoundedBuffer[String](10); + var cnt = 0; + def produceString = { cnt = cnt + 1; cnt.toString() } + def consumeString(ss: String) = System.out.println(ss); + spawn { while (true) { val ssss = produceString; buf.put(ssss) } } + spawn { while (true) { val s = buf.get; consumeString(s) } } } -} -module test { - val buf = new BoundedBuffer[String](10); - var cnt = 0; - def produceString = { cnt = cnt + 1; cnt.toString() } - def consumeString(ss: String) = System.out.println(ss); - spawn { while (True) { val ssss = produceString ; buf.put(ssss) } } - spawn { while (True) { val s = buf.get ; consumeString(s) } } } diff --git a/sources/examples/buffer1.scala b/sources/examples/buffer1.scala index 40be8431be..2afb4377bf 100644 --- a/sources/examples/buffer1.scala +++ b/sources/examples/buffer1.scala @@ -1,16 +1,46 @@ package examples; -import scala.concurrent._; +object buffer1 { -class OnePlaceBuffer() { - private val m = new MailBox(); // An internal mailbox - private case class Empty(), Full(x: Int); // Types of messages we deal with + import scala.concurrent._; - m send Empty(); // Initialization + class OnePlaceBuffer { + private val m = new MailBox(); // An internal mailbox + private case class Empty(), Full(x: Int); // Types of messages we deal with - def write(x: Int): Unit = - m receive { case Empty() => m send Full(x) } + m send Empty(); // Initialization + + def write(x: Int): Unit = m receive { + case Empty() => + Console.println("put " + x); + m send Full(x) + } + + def read: Int = m receive { + case Full(x) => + Console.println("get " + x); + m send Empty() ; x + } + } + + def main(args: Array[String]) = { + val buf = new OnePlaceBuffer; + val random = new java.util.Random(); + + def producer(n: int): unit = { + Thread.sleep(random.nextInt(1000)); + buf.write(n); + producer(n + 1) + } + + def consumer: unit = { + Thread.sleep(random.nextInt(1000)); + val n = buf.read; + consumer + } + + ops.spawn(producer(0)); + ops.spawn(consumer) + } - def read: Int = - m receive { case Full(x) => m send Empty() ; x } } diff --git a/sources/examples/fors.scala b/sources/examples/fors.scala index cf22c2830e..ca817dcdc4 100644 --- a/sources/examples/fors.scala +++ b/sources/examples/fors.scala @@ -1,3 +1,5 @@ +package examples; + object fors { class Person(_name: String, _age: Int) { @@ -80,11 +82,11 @@ object fors { findNums(15) foreach { x => Console.print(" " + x); }; Console.println; - val xs = List(3.5, 5.2); + val xs = List(3.5, 5.0, 4.5); Console.println("average(" + xs + ") = " + sum(xs) / xs.length); - val ys = List(2.0, 1.0); + val ys = List(2.0, 1.0, 3.0); Console.println("scalProd(" + xs + ", " + ys +") = " + scalProd(xs, ys)); } diff --git a/sources/examples/iterators.scala b/sources/examples/iterators.scala index 8eae9fb026..958934e65e 100644 --- a/sources/examples/iterators.scala +++ b/sources/examples/iterators.scala @@ -1,6 +1,6 @@ -module iterators { +object iterators { - def printArray(xs: Array[Int]) = + def printArray(xs: Array[Double]) = Iterator.fromArray(xs) foreach (x => System.out.println(x)); def findGreater(xs: Array[Double], limit: Double) = @@ -9,4 +9,11 @@ module iterators { .filter{case Pair(x, i) => x > limit} .map{case Pair(x, i) => i} + def main(args: Array[String]) = { + val xs = List(6, 2, 8, 5, 1); + val ar = xs.copyToArray(new Array[Double](xs.length), 0); + printArray(ar); + findGreater(ar, 3.0) foreach { x => Console.print(x); }; + } + } diff --git a/sources/examples/maps.scala b/sources/examples/maps.scala index 2f9471d2dd..a5b2348f95 100644 --- a/sources/examples/maps.scala +++ b/sources/examples/maps.scala @@ -1,4 +1,8 @@ -module maps { +package examples; + +object maps { + + import scala.collection.immutable._; trait MapStruct[kt, vt] { trait Map with Function1[kt, vt] { @@ -146,6 +150,28 @@ module maps { } val empty = new MutMap(null, null); } + + class Date(y: Int, m: Int, d: Int) with Ord[Date] { + def year = y, month = m, day = d; + + def <(that: Date): Boolean = { + (year < that.year) || + (year == that.year && month < that.month) || + (month == that.month && day < that.day) + } + + override def equals(that: Any): Boolean = + that.isInstanceOf[Date] && { + val o = that.asInstanceOf[Date]; + day == o.day && month == o.month && year == o.year + } + } + + def main(args: Array[String]) = { + val t = new OOBinTree[Date, String](); + () + } + } diff --git a/sources/examples/sort.scala b/sources/examples/sort.scala index 56dcadb76b..2c3c3666a5 100644 --- a/sources/examples/sort.scala +++ b/sources/examples/sort.scala @@ -1,3 +1,5 @@ +package examples; + object sorter { def sort(a: Array[Int]): Unit = { @@ -40,7 +42,7 @@ object sorter { val ar = xs.copyToArray(new Array[Int](xs.length), 0); println(ar); sort(ar); - println(ar); + println(ar) } } diff --git a/sources/examples/sort1.scala b/sources/examples/sort1.scala index 13a7b3e568..77fa36a052 100644 --- a/sources/examples/sort1.scala +++ b/sources/examples/sort1.scala @@ -1,3 +1,5 @@ +package examples; + object sorter { def sort(a: List[Int]): List[Int] = { @@ -14,7 +16,7 @@ object sorter { def main(args: Array[String]) = { val xs = List(6, 2, 8, 5, 1); Console.println(xs); - Console.println(sort(xs)); + Console.println(sort(xs)) } -} \ No newline at end of file +} diff --git a/sources/examples/sort2.scala b/sources/examples/sort2.scala index f143958471..67998bfb80 100644 --- a/sources/examples/sort2.scala +++ b/sources/examples/sort2.scala @@ -1,3 +1,5 @@ +package examples; + object sorter { def sort(a: List[Int]): List[Int] = { @@ -17,7 +19,7 @@ object sorter { def main(args: Array[String]) = { val xs = List(6, 2, 8, 5, 1); Console.println(xs); - Console.println(sort(xs)); + Console.println(sort(xs)) } -} \ No newline at end of file +} diff --git a/sources/examples/typeinf.scala b/sources/examples/typeinf.scala index af94f9d3c4..0a3a96819e 100644 --- a/sources/examples/typeinf.scala +++ b/sources/examples/typeinf.scala @@ -6,7 +6,7 @@ case class Lam(x: String, e: Term) extends Term {} case class App(f: Term, e: Term) extends Term {} case class Let(x: String, e: Term, f: Term) extends Term {} -module types { +object types { trait Type {} case class Tyvar(a: String) extends Type {} case class Arrow(t1: Type, t2: Type) extends Type {} @@ -19,7 +19,7 @@ import types._; case class ListSet[a](elems: List[a]) { def contains(y: a): Boolean = elems match { - case List() => False + case List() => false case x :: xs => (x == y) || (xs contains y) } @@ -38,7 +38,7 @@ case class ListSet[a](elems: List[a]) { } } -module typeInfer { +object typeInfer { trait Subst with Function1[Type,Type] { def lookup(x: Tyvar): Type; @@ -73,7 +73,7 @@ module typeInfer { case class TypeScheme(vs: List[String], t: Type) { def newInstance: Type = - (emptySubst foldl_: vs) { (s, a) => s.extend(Tyvar(a), newTyvar) } (t); + vs.foldLeft(emptySubst) { (s, a) => s.extend(Tyvar(a), newTyvar) } (t); } type Env = List[Pair[String, TypeScheme]]; @@ -97,7 +97,7 @@ module typeInfer { case Pair(Arrow(t1, t2), Arrow(u1, u2)) => mgu(t1, u1)(mgu(t2, u2)(s)) case Pair(Tycon(k1, ts), Tycon(k2, us)) if (k1 == k2) => - (s foldl_: ((ts zip us) map {case Pair(t,u) => mgu(t,u)})) { (s, f) => f(s) } + ((ts zip us) map {case Pair(t,u) => mgu(t,u)}).foldLeft(s) { (s, f) => f(s) } case _ => error("unification failure"); } @@ -131,7 +131,7 @@ module typeInfer { } } -module predefined { +object predefined { val booleanType = Tycon("Boolean", List()); val intType = Tycon("Int", List()); def listType(t: Type) = Tycon("List", List(t)); @@ -139,6 +139,7 @@ module predefined { private def gen(t: Type): typeInfer.TypeScheme = typeInfer.gen(List(), t); private val a = newTyvar; val env = List( +/* Pair("true", gen(booleanType)), Pair("false", gen(booleanType)), Pair("if", gen(Arrow(booleanType, Arrow(a, Arrow(a, a))))), @@ -149,14 +150,16 @@ module predefined { Pair("isEmpty", gen(Arrow(listType(a), booleanType))), Pair("head", gen(Arrow(listType(a), a))), Pair("tail", gen(Arrow(listType(a), listType(a)))), +*/ Pair("fix", gen(Arrow(Arrow(a, a), a))) ) } -module test { +object test with Executable { - def showType(e: Term) = typeInfer.typeOf(predefined.env, e); - - showType(Lam("x", App(App(Var("cons"), Var("x")), Var("nil")))); +// def showType(e: Term) = typeInfer.typeOf(predefined.env, e); + def showType(e: Term) = typeInfer.typeOf(Nil, e); + Console.println( + showType(Lam("x", App(App(Var("cons"), Var("x")), Var("nil"))))); } -- cgit v1.2.3