diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-16 18:44:33 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-16 18:44:33 +0000 |
commit | 53a3cc7b17f4cf97075b7e71720777fd84109696 (patch) | |
tree | 0cc784e0b47ea49cc151a136d19f20bfa8ee2197 /sources/examples/fors.scala | |
parent | df50e05006b43b007c2587549030d24b5c154398 (diff) | |
download | scala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.gz scala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.bz2 scala-53a3cc7b17f4cf97075b7e71720777fd84109696.zip |
Created proper 'docs' folder for new layout.
Diffstat (limited to 'sources/examples/fors.scala')
-rw-r--r-- | sources/examples/fors.scala | 114 |
1 files changed, 0 insertions, 114 deletions
diff --git a/sources/examples/fors.scala b/sources/examples/fors.scala deleted file mode 100644 index 6c969d3519..0000000000 --- a/sources/examples/fors.scala +++ /dev/null @@ -1,114 +0,0 @@ -package examples; - -import scala.xml._; - - -object fors { - - val e = Node.NoAttributes ; - - class Person(_name: String, _age: Int) { - val name = _name; - val age = _age; - } - - def printOlderThan20(xs: Seq[Person]): Iterator[String] = - printOlderThan20(xs.elements); - - def printOlderThan20(xs: Iterator[Person]): Iterator[String] = - 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; - - 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); - - 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); - - type Lst = List[Any]; - - val prefix = null; - val scope = TopScope; - - val books = List( - Elem(prefix, "book", e, scope, - Elem(prefix, "title", e, scope, - Text("Structure and Interpretation of Computer Programs")), - Elem(prefix, "author", e, scope, - Text("Abelson, Harald")), - Elem(prefix, "author", e, scope, - Text("Sussman, Gerald J."))), - Elem(prefix, "book", e, scope, - Elem(prefix, "title", e, scope, - Text("Principles of Compiler Design")), - Elem(prefix, "author", e, scope, - Text("Aho, Alfred")), - Elem(prefix, "author", e, scope, - Text("Ullman, Jeffrey"))), - Elem(prefix, "book", e, scope, - Elem(prefix, "title", e, scope, - Text("Programming in Modula-2")), - 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; - (title indexOf "Program") >= 0; - val Elem(_, "author", _, scope, Text(author)) <- book) yield author; - - for (val Elem(_, "book", _, scope, b @ _*) <- books; - val Elem(_, "author", _, scope, Text(author)) <- b; - author startsWith "Ullman"; - val Elem(_, "title", _, scope, Text(title)) <- b) 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)); - - 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); - - def main(args: Array[String]) = { - Console.print("Persons over 20:"); - printOlderThan20(persons) foreach { x => Console.print(" " + x) }; - Console.println; - - Console.println("divisors(34) = " + divisors(34)); - - Console.print("findNums(15) ="); - 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 ys = List(2.0, 1.0, 3.0); - Console.println("scalProd(" + xs + ", " + ys +") = " - + scalProd(xs, ys)); - } - -} |