From c44a597469fcd3549618348ad9808d923a1c9c2c Mon Sep 17 00:00:00 2001 From: michelou Date: Wed, 8 Mar 2006 10:08:03 +0000 Subject: adapted some more examples to Scala 2 --- docs/examples/computeserver.scala | 4 +- docs/examples/monads/errorInterpreter.scala | 70 ++++++++++++++--------------- docs/examples/patterns.scala | 23 +++++----- 3 files changed, 49 insertions(+), 48 deletions(-) (limited to 'docs') diff --git a/docs/examples/computeserver.scala b/docs/examples/computeserver.scala index 7e63455258..96d7e6560a 100644 --- a/docs/examples/computeserver.scala +++ b/docs/examples/computeserver.scala @@ -1,6 +1,6 @@ -package examples; +package examples -import concurrent._, concurrent.ops._; +import concurrent._, concurrent.ops._ class ComputeServer(n: Int) { diff --git a/docs/examples/monads/errorInterpreter.scala b/docs/examples/monads/errorInterpreter.scala index 05dc979621..4edb5caf88 100644 --- a/docs/examples/monads/errorInterpreter.scala +++ b/docs/examples/monads/errorInterpreter.scala @@ -1,47 +1,47 @@ object errorInterpreter { trait M[A] { - def show: String; - def bind[B](k: A => M[B]); - def map[B](f: A => B): M[B] = bind(x => unitM(f(x))); - def flatMap[B](f: A => M[B]): M[B] = bind(f); + def show: String + def bind[B](k: A => M[B]): M[B] + def map[B](f: A => B): M[B] = bind(x => unitM(f(x))) + def flatMap[B](f: A => M[B]): M[B] = bind(f) } - def unitM[A](a: A): M[A] = new Suc(a); - def errorM[A](msg: String): M[A] = new Err(msg); + def unitM[A](a: A): M[A] = new Suc(a) + def errorM[A](msg: String): M[A] = new Err(msg) - def showM(m: M[Value]): String = m.show; + def showM(m: M[Value]): String = m.show - class Suc[A](x: T) extends M[A] { - def bind[A](k: A => M[B]): M[B] = k(x); - def show: String = "Success: " + x; + class Suc[A](x: A) extends M[A] { + def bind[B](k: A => M[B]): M[B] = k(x) + def show: String = "Success: " + x } - class Err[T](msg: String) extends M[T] { - def bind[A](k: A => M[B]): M[B] = new Err(msg); - def show: String = "Error: " + msg; + class Err[A](msg: String) extends M[A] { + def bind[B](k: A => M[B]): M[B] = new Err(msg) + def show: String = "Error: " + msg } - type Name = String; + type Name = String - trait Term; - case class Var(x: Name) extends Term; - case class Con(n: int) extends Term; - case class Add(l: Term, r: Term) extends Term; - case class Lam(x: Name, body: Term) extends Term; - case class App(fun: Term, arg: Term) extends Term; + trait Term + case class Var(x: Name) extends Term + case class Con(n: int) extends Term + case class Add(l: Term, r: Term) extends Term + case class Lam(x: Name, body: Term) extends Term + case class App(fun: Term, arg: Term) extends Term - trait Value; + trait Value case object Wrong extends Value { - override def toString() = "wrong" + override def toString() = "wrong" } case class Num(n: int) extends Value { - override def toString() = n.toString(); + override def toString() = n.toString() } case class Fun(f: Value => M[Value]) extends Value { override def toString() = "" } - type Environment = List[Pair[Name, Value]]; + type Environment = List[Pair[Name, Value]] def lookup(x: Name, e: Environment): M[Value] = e match { case List() => errorM("unbound variable: " + x); @@ -55,32 +55,32 @@ object errorInterpreter { def apply(a: Value, b: Value): M[Value] = a match { case Fun(k) => k(b) - case _ => errorM("should be function: " + a); + case _ => errorM("should be function: " + a) } def interp(t: Term, e: Environment): M[Value] = t match { case Var(x) => lookup(x, e) case Con(n) => unitM(Num(n)) case Add(l, r) => for (val a <- interp(l, e); - val b <- interp(r, e); - val c <- add(a, b)) + val b <- interp(r, e); + val c <- add(a, b)) yield c case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e))) case App(f, t) => for (val a <- interp(f, e); - val b <- interp(t, e); - val c <- apply(a, b)) - yield c + val b <- interp(t, e); + val c <- apply(a, b)) + yield c } def test(t: Term): String = - showM(interp(t, List())); + showM(interp(t, List())) - val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11))); - val term1 = App(Con(1), Con(2)); + val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11))) + val term1 = App(Con(1), Con(2)) def main(args: Array[String]) = { - System.out.println(test(term0)); - System.out.println(test(term1)); + System.out.println(test(term0)) + System.out.println(test(term1)) } } diff --git a/docs/examples/patterns.scala b/docs/examples/patterns.scala index 34b0db96ae..e36149ad9c 100644 --- a/docs/examples/patterns.scala +++ b/docs/examples/patterns.scala @@ -1,12 +1,12 @@ -package examples; +package examples object patterns { - trait Tree; - case class Branch(left: Tree, right: Tree) extends Tree; - case class Leaf(x: Int) extends Tree; + trait Tree + case class Branch(left: Tree, right: Tree) extends Tree + case class Leaf(x: Int) extends Tree - val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4))); + val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4))) def sumLeaves(t: Tree): Int = t match { case Branch(l, r) => sumLeaves(l) + sumLeaves(r) @@ -14,12 +14,13 @@ object patterns { } def find[a,b](it: Iterator[Pair[a, b]], x: a): Option[b] = { - var result: Option[b] = _; - while (it.hasNext && result == null) { - val Pair(x1, y) = it.next; - if (x == x1) result = Some(y) + var result: Option[b] = None + var found = false + while (it.hasNext && !found) { + val Pair(x1, y) = it.next + if (x == x1) { found = true; result = Some(y) } } - if (result == null) None else result + result } def printFinds[a](xs: List[Pair[a, String]], x: a) = @@ -29,7 +30,7 @@ object patterns { } def main(args: Array[String]): Unit = { - Console.println("sum of leafs=" + sumLeaves(tree1)); + Console.println("sum of leafs=" + sumLeaves(tree1)) printFinds(List(Pair(3, "three"), Pair(4, "four")), 4) } } \ No newline at end of file -- cgit v1.2.3