From 2f0f432ebcbbdf6fb25c8f500ee2fffe6b8ed025 Mon Sep 17 00:00:00 2001 From: michelou Date: Tue, 10 Oct 2006 16:00:00 +0000 Subject: updated docs/examples/*.scala --- docs/examples/fors.scala | 82 +++++++++++++++++++++----------------------- docs/examples/maps.scala | 46 ++++++++++++------------- docs/examples/parsers1.scala | 12 +++---- 3 files changed, 67 insertions(+), 73 deletions(-) (limited to 'docs/examples') diff --git a/docs/examples/fors.scala b/docs/examples/fors.scala index 6c969d3519..3de4ba52b7 100644 --- a/docs/examples/fors.scala +++ b/docs/examples/fors.scala @@ -1,48 +1,48 @@ -package examples; +package examples -import scala.xml._; +import scala.xml._ object fors { - val e = Node.NoAttributes ; + val e = Node.NoAttributes class Person(_name: String, _age: Int) { - val name = _name; - val age = _age; + val name = _name + val age = _age } def printOlderThan20(xs: Seq[Person]): Iterator[String] = - printOlderThan20(xs.elements); + printOlderThan20(xs.elements) def printOlderThan20(xs: Iterator[Person]): Iterator[String] = - for (val p <- xs; p.age > 20) yield p.name; + for (val p <- xs; p.age > 20) yield p.name val persons = List( new Person("John", 40), new Person("Richard", 68) - ); + ) def divisors(n: Int): List[Int] = - for (val i <- List.range(1, n+1); n % i == 0) yield i; + for (val i <- List.range(1, n+1); n % i == 0) yield i - def isPrime(n: Int) = divisors(n).length == 2; + def isPrime(n: Int) = divisors(n).length == 2 def findNums(n: Int): Iterator[Pair[Int, Int]] = for (val i <- Iterator.range(1, n); val j <- Iterator.range(1, i-1); - isPrime(i+j)) yield Pair(i, j); + isPrime(i+j)) yield Pair(i, j) def sum(xs: List[Double]): Double = xs.foldLeft(0.0) { (x, y) => x + y } def scalProd(xs: List[Double], ys: List[Double]) = - sum(for(val Pair(x, y) <- xs zip ys) yield x * y); + sum(for(val Pair(x, y) <- xs zip ys) yield x * y) - type Lst = List[Any]; + type Lst = List[Any] - val prefix = null; - val scope = TopScope; + val prefix = null + val scope = TopScope val books = List( Elem(prefix, "book", e, scope, @@ -62,53 +62,51 @@ object fors { Elem(prefix, "book", e, scope, Elem(prefix, "title", e, scope, Text("Programming in Modula-2")), - Elem(prefix, "author", e, scope, + Elem(prefix, "author", e, scope, Text("Wirth, Niklaus"))) - ); + ) def findAuthor(books: Lst) = - for (val Elem(_, "book", _, scope, book @ _*) <- books; - val Elem(_, "title", _, scope, Text(title)) <- book; + for (val Elem(_, "book", _, _, book @ _*) <- books; + val Elem(_, "title", _, _, Text(title)) <- book.toList; (title indexOf "Program") >= 0; - val Elem(_, "author", _, scope, Text(author)) <- book) yield author; + val Elem(_, "author", _, _, Text(author)) <- List(book)) yield author - for (val Elem(_, "book", _, scope, b @ _*) <- books; - val Elem(_, "author", _, scope, Text(author)) <- b; + for (val Elem(_, "book", _, _, book @ _*) <- books; + val Elem(_, "author", _, _, Text(author)) <- book.toList; author startsWith "Ullman"; - val Elem(_, "title", _, scope, Text(title)) <- b) yield title; + val Elem(_, "title", _, _, Text(title)) <- List(book)) yield title removeDuplicates( - for (val Elem(_, "book", _, scope, b1 @ _* ) <- books; - val Elem(_, "book", _, scope, b2 @ _*) <- books; - b1 != b2; - val Elem(_, "author", _, scope, Text(a1)) <- b1; - val Elem(_, "author", _, scope, Text(a2)) <- b2; - a1 == a2) yield Pair(a1, a2)); + for (val Elem(_, "book", _, _, b1 @ _* ) <- books; + val Elem(_, "book", _, _, b2 @ _*) <- books; + b1 != b2; + val Elem(_, "author", _, _, Text(a1)) <- b1.toList; + val Elem(_, "author", _, _, Text(a2)) <- b2.toList; + a1 == a2) yield Pair(a1, a2)) def removeDuplicates[a](xs: List[a]): List[a] = if (xs.isEmpty) xs else - xs.head :: removeDuplicates(for (val x <- xs.tail; x != xs.head) yield x); + xs.head :: removeDuplicates(for (val x <- xs.tail; x != xs.head) yield x) def main(args: Array[String]) = { - Console.print("Persons over 20:"); - printOlderThan20(persons) foreach { x => Console.print(" " + x) }; - Console.println; + Console.print("Persons over 20:") + printOlderThan20(persons) foreach { x => Console.print(" " + x) } + Console.println - Console.println("divisors(34) = " + divisors(34)); + Console.println("divisors(34) = " + divisors(34)) Console.print("findNums(15) ="); - findNums(15) foreach { x => Console.print(" " + x); }; - Console.println; + findNums(15) foreach { x => Console.print(" " + x); } + Console.println - val xs = List(3.5, 5.0, 4.5); - Console.println("average(" + xs + ") = " - + sum(xs) / xs.length); + val xs = List(3.5, 5.0, 4.5) + Console.println("average(" + xs + ") = " + sum(xs) / xs.length) - val ys = List(2.0, 1.0, 3.0); - Console.println("scalProd(" + xs + ", " + ys +") = " - + scalProd(xs, ys)); + val ys = List(2.0, 1.0, 3.0) + Console.println("scalProd(" + xs + ", " + ys +") = " + scalProd(xs, ys)) } } diff --git a/docs/examples/maps.scala b/docs/examples/maps.scala index 1b5d41df54..5b1af51ac3 100644 --- a/docs/examples/maps.scala +++ b/docs/examples/maps.scala @@ -90,18 +90,18 @@ object maps { def extend(key: kt, value: vt): map = if (key < k) new Node(k, v, l.extend(key, value), r) else if (key > k) new Node(k, v, l, r.extend(key, value)) - else new Node(k, value, l, r); + else new Node(k, value, l, r) def remove(key: kt): map = if (key < k) new Node(k, v, l.remove(key), r) else if (key > k) new Node(k, v, l, r.remove(key)) else if (l == empty) r else if (r == empty) l else { - val midKey = r.domain.head; + val midKey = r.domain.head new Node(midKey, r(midKey), l, r.remove(midKey)) } - def domain: Stream[kt] = l.domain append Stream.cons(k, r.domain); - def range: Stream[vt] = l.range append Stream.cons(v, r.range); + def domain: Stream[kt] = l.domain append Stream.cons(k, r.domain) + def range: Stream[vt] = l.range append Stream.cons(v, r.range) } } @@ -129,24 +129,24 @@ object maps { def remove(key: kt): map = if (this == empty) this - else if (key < k) { l = l.remove(key) ; this } - else if (key > k) { r = r.remove(key) ; this } + else if (key < k) { l = l.remove(key); this } + else if (key > k) { r = r.remove(key); this } else if (l == empty) r else if (r == empty) l else { var mid = r while (!(mid.l == empty)) { mid = mid.l } - mid.r = r.remove(mid.k); - mid.l = l; + mid.r = r.remove(mid.k) + mid.l = l mid } def domain: Stream[kt] = - if (this == empty) Stream.empty; + if (this == empty) Stream.empty else l.domain append Stream.cons(k, r.domain) def range: Stream[vt] = - if (this == empty) Stream.empty; + if (this == empty) Stream.empty else l.range append Stream.cons(v, r.range) } val empty = new MutMap(null, null) @@ -157,21 +157,17 @@ object maps { def month = m def day = d - override def compare [b >: Date <% Ordered[b]](that: b): Int = that match { - case other: Date => - if ((year == other.year) && - (month == other.month) && - (day == other.day)) - 0 - else if ((year < other.year) || - (year == other.year && month < other.month) || - (month == other.month && day < other.day)) - -1 - else - 1 - case _ => - -(that compareTo this) - } + def compare(other: Date): Int = + if (year == other.year && + month == other.month && + day == other.day) + 0 + else if (year < other.year || + year == other.year && month < other.month || + month == other.month && day < other.day) + -1 + else + 1 override def equals(that: Any): Boolean = that.isInstanceOf[Date] && { diff --git a/docs/examples/parsers1.scala b/docs/examples/parsers1.scala index cdd68470d4..c9029ffb11 100644 --- a/docs/examples/parsers1.scala +++ b/docs/examples/parsers1.scala @@ -1,4 +1,4 @@ -package examples; +package examples object parsers1 { @@ -46,10 +46,10 @@ object parsers1 { trait ListParsers extends Parsers { def chr(p: char => boolean): Parser - def chr(c: char): Parser = chr(d: char => d == c) + def chr(c: char): Parser = chr((d: char) => d == c) - def letter : Parser = chr(c: char => Character.isLetter(c)) - def digit : Parser = chr(c: char => Character.isDigit(c)) + def letter : Parser = chr((c: char) => Character.isLetter(c)) + def digit : Parser = chr((c: char) => Character.isDigit(c)) def ident : Parser = letter &&& rep(letter ||| digit) def number : Parser = digit &&& rep(digit) @@ -60,9 +60,9 @@ object parsers1 { trait ExprParsers extends Parsers { def chr(p: char => boolean): Parser - def chr(c: char): Parser = chr(d: char => d == c) + def chr(c: char): Parser = chr((d: char) => d == c) - def digit : Parser = chr(c: char => Character.isDigit(c)) + def digit : Parser = chr((c: char) => Character.isDigit(c)) def number : Parser = digit &&& rep(digit) def summand : Parser = number ||| chr('(') &&& expr &&& chr(')') def expr : Parser = summand &&& rep(chr('+') &&& summand) -- cgit v1.2.3