Updated examples wrt. to new for-comprehension syntax.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@10846 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
dragos 2007-04-26 13:37:47 +00:00
parent 332c32dc71
commit 07c4074dfa
11 changed files with 87 additions and 87 deletions

View File

@ -39,7 +39,7 @@ abstract class Parsers {
}
def &&& [b](p: => Parser[b]): Parser[b] =
for (val _ <- this; val x <- p) yield x
for (_ <- this; x <- p) yield x
}
def succeed[a](x: a) = new Parser[a] {
@ -50,10 +50,10 @@ abstract class Parsers {
rep1(p) ||| succeed(List())
def rep1[a](p: Parser[a]): Parser[List[a]] =
for (val x <- p; val xs <- rep(p)) yield x :: xs
for (x <- p; xs <- rep(p)) yield x :: xs
def opt[a](p: Parser[a]): Parser[List[a]] =
(for (val x <- p) yield List(x)) ||| succeed(List())
(for (x <- p) yield List(x)) ||| succeed(List())
}
class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] {
@ -102,7 +102,7 @@ trait TokenParsers extends Parsers {
trait CharParsers extends Parsers {
def any: Parser[char]
def chr(ch: char) =
for (val c <- any; c == ch) yield c
for (c <- any; if c == ch) yield c
def chr(p: char => boolean) =
for (val c <- any; p(c)) yield c
for (c <- any; if p(c)) yield c
}

View File

@ -102,11 +102,11 @@ object producers extends Application {
actor {
Console.print("PreOrder:")
for (val x <- new PreOrder(tree).iterator) Console.print(" "+x)
for (x <- new PreOrder(tree).iterator) Console.print(" "+x)
Console.print("\nPostOrder:")
for (val x <- new PostOrder(tree).iterator) Console.print(" "+x)
for (x <- new PostOrder(tree).iterator) Console.print(" "+x)
Console.print("\nInOrder:")
for (val x <- new InOrder(tree).iterator) Console.print(" "+x)
for (x <- new InOrder(tree).iterator) Console.print(" "+x)
Console.print("\n")
}
}

View File

@ -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:")

View File

@ -4,7 +4,7 @@ object iterators {
def Array(elems: Double*): Array[Double] = {
val ar = new Array[Double](elems.length)
for (val i <- Iterator.range(0, elems.length))
for (i <- Iterator.range(0, elems.length))
ar(i) = elems(i)
ar
}

View File

@ -58,14 +58,14 @@ object callccInterpreter {
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))
case Add(l, r) => for (a <- interp(l, e);
b <- interp(r, e);
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))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
case Ccc(x, t) => callCC(k => interp(t, Pair(x, Fun(k)) :: e))
}

View File

@ -61,14 +61,14 @@ object errorInterpreter {
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))
case Add(l, r) => for (a <- interp(l, e);
b <- interp(r, e);
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))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
}

View File

@ -50,14 +50,14 @@ object simpleInterpreter {
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))
case Add(l, r) => for (a <- interp(l, e);
b <- interp(r, e);
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))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
}

View File

@ -49,26 +49,26 @@ object stateInterpreter {
}
def add(a: Value, b: Value): M[Value] = Pair(a, b) match {
case Pair(Num(m), Num(n)) => for (val _ <- tickS) yield Num(m + n)
case Pair(Num(m), Num(n)) => for (_ <- tickS) yield Num(m + n)
case _ => unitM(Wrong)
}
def apply(a: Value, b: Value): M[Value] = a match {
case Fun(k) => for (val _ <- tickS; val c <- k(b)) yield c
case Fun(k) => for (_ <- tickS; c <- k(b)) yield c
case _ => unitM(Wrong)
}
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))
case Add(l, r) => for (a <- interp(l, e);
b <- interp(r, e);
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))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
}

View File

@ -15,27 +15,27 @@ object parsers2 {
def ident: Parser[Tree] =
for (
val c: char <- chr(isLetter);
val cs: List[char] <- rep(chr(isLetterOrDigit))
c: char <- chr(isLetter);
cs: List[char] <- rep(chr(isLetterOrDigit))
) yield Id((c :: cs).mkString("", "", ""))
def number: Parser[Tree] =
for (
val d: char <- chr(isDigit);
val ds: List[char] <- rep(chr(isDigit))
d: char <- chr(isDigit);
ds: List[char] <- rep(chr(isDigit))
) yield Num(((d - '0') /: ds) ((x, digit) => x * 10 + digit - '0'))
def list: Parser[Tree] =
for (
val _ <- chr('(');
val es <- listElems ||| succeed(List());
val _ <- chr(')')
_ <- chr('(');
es <- listElems ||| succeed(List());
_ <- chr(')')
) yield Lst(es)
def listElems: Parser[List[Tree]] =
for (
val x <- expr;
val xs <- chr(',') &&& listElems ||| succeed(List())
x <- expr;
xs <- chr(',') &&& listElems ||| succeed(List())
) yield x :: xs
def expr: Parser[Tree] =

View File

@ -163,55 +163,55 @@ object typeInfer {
/** identifiers or keywords */
def id: Parser[String] =
for (
val c: char <- rep(chr(' ')) &&& chr(isLetter);
val cs: List[char] <- rep(chr(isLetterOrDigit))
c: char <- rep(chr(' ')) &&& chr(isLetter);
cs: List[char] <- rep(chr(isLetterOrDigit))
) yield (c :: cs).mkString("", "", "")
/** Non-keyword identifiers */
def ident: Parser[String] =
for (val s <- id; s != "let" && s != "in") yield s
for (s <- id; if s != "let" && s != "in") yield s
/** term = '\' ident '.' term | term1 {term1} | let ident "=" term in term */
def term: Parser[Term] = (
( for (
val _ <- wschr('\\');
val x <- ident;
val _ <- wschr('.');
val t <- term)
_ <- wschr('\\');
x <- ident;
_ <- wschr('.');
t <- term)
yield Lam(x, t): Term )
|||
( for (
val letid <- id; letid == "let";
val x <- ident;
val _ <- wschr('=');
val t <- term;
val inid <- id; inid == "in";
val c <- term)
letid <- id; if letid == "let";
x <- ident;
_ <- wschr('=');
t <- term;
inid <- id; if inid == "in";
c <- term)
yield Let(x, t, c) )
|||
( for (
val t <- term1;
val ts <- rep(term1))
t <- term1;
ts <- rep(term1))
yield (t /: ts)((f, arg) => App(f, arg)) )
)
/** term1 = ident | '(' term ')' */
def term1: Parser[Term] = (
( for (val s <- ident)
( for (s <- ident)
yield Var(s): Term )
|||
( for (
val _ <- wschr('(');
val t <- term;
val _ <- wschr(')'))
_ <- wschr('(');
t <- term;
_ <- wschr(')'))
yield t )
)
/** all = term ';' */
def all: Parser[Term] =
for (
val t <- term;
val _ <- wschr(';'))
t <- term;
_ <- wschr(';'))
yield t
}

View File

@ -41,7 +41,7 @@ object phonebook3 {
/** walks through tree, returns changed/copied updated tree */
def copyOrChange ( ch: Iterator[Node] ):List[Node] = {
for( val c <- ch ) yield c match {
for(c <- ch ) yield c match {
case x @ <entry>{ ch1 @ _* }</entry> if hasName(x) =>
val it = ch1.elements;