diff options
author | Iulian Dragos <jaguarul@gmail.com> | 2007-04-26 13:37:47 +0000 |
---|---|---|
committer | Iulian Dragos <jaguarul@gmail.com> | 2007-04-26 13:37:47 +0000 |
commit | 8d2349581f5694704fb1bebc713339d46823ec67 (patch) | |
tree | 44be4d4b13a50c44a22e3310e9a2b24baa9cbf44 /docs/examples/fors.scala | |
parent | 99630e293745f07ac3e88582872230c015a4f963 (diff) | |
download | scala-8d2349581f5694704fb1bebc713339d46823ec67.tar.gz scala-8d2349581f5694704fb1bebc713339d46823ec67.tar.bz2 scala-8d2349581f5694704fb1bebc713339d46823ec67.zip |
Updated examples wrt.
Diffstat (limited to 'docs/examples/fors.scala')
-rw-r--r-- | docs/examples/fors.scala | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/docs/examples/fors.scala b/docs/examples/fors.scala index 3de4ba52b7..b36ac2b60f 100644 --- a/docs/examples/fors.scala +++ b/docs/examples/fors.scala @@ -16,7 +16,7 @@ object fors { printOlderThan20(xs.elements) def printOlderThan20(xs: Iterator[Person]): Iterator[String] = - for (val p <- xs; p.age > 20) yield p.name + for (p <- xs; if p.age > 20) yield p.name val persons = List( new Person("John", 40), @@ -24,20 +24,20 @@ object fors { ) def divisors(n: Int): List[Int] = - for (val i <- List.range(1, n+1); n % i == 0) yield i + for (i <- List.range(1, n+1); if 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) + for (i <- Iterator.range(1, n); + j <- Iterator.range(1, i-1); + if 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((x, y) <- xs zip ys) yield x * y) type Lst = List[Any] @@ -67,29 +67,29 @@ object fors { ) def findAuthor(books: Lst) = - for (val Elem(_, "book", _, _, book @ _*) <- books; - val Elem(_, "title", _, _, Text(title)) <- book.toList; - (title indexOf "Program") >= 0; - val Elem(_, "author", _, _, Text(author)) <- List(book)) yield author + for (Elem(_, "book", _, _, book @ _*) <- books; + Elem(_, "title", _, _, Text(title)) <- book.toList; + if (title indexOf "Program") >= 0; + Elem(_, "author", _, _, Text(author)) <- List(book)) yield author - for (val Elem(_, "book", _, _, book @ _*) <- books; - val Elem(_, "author", _, _, Text(author)) <- book.toList; - author startsWith "Ullman"; - val Elem(_, "title", _, _, Text(title)) <- List(book)) yield title + for (Elem(_, "book", _, _, book @ _*) <- books; + Elem(_, "author", _, _, Text(author)) <- book.toList; + if author startsWith "Ullman"; + Elem(_, "title", _, _, Text(title)) <- List(book)) yield title removeDuplicates( - 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)) + for (Elem(_, "book", _, _, b1 @ _* ) <- books; + Elem(_, "book", _, _, b2 @ _*) <- books; + if b1 != b2; + Elem(_, "author", _, _, Text(a1)) <- b1.toList; + Elem(_, "author", _, _, Text(a2)) <- b2.toList; + if 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 (x <- xs.tail; if x != xs.head) yield x) def main(args: Array[String]) = { Console.print("Persons over 20:") |