diff options
author | Iulian Dragos <jaguarul@gmail.com> | 2010-04-13 22:51:04 +0000 |
---|---|---|
committer | Iulian Dragos <jaguarul@gmail.com> | 2010-04-13 22:51:04 +0000 |
commit | d95eb2a8f979b9f9db98a0e097ef3793674e57ab (patch) | |
tree | 6e11ba6e02cdd30c53861d42e6e52d5ce34b8d95 /test/files/run | |
parent | c272bbfb64b1d0522e09daa0981a4942f63607a4 (diff) | |
download | scala-d95eb2a8f979b9f9db98a0e097ef3793674e57ab.tar.gz scala-d95eb2a8f979b9f9db98a0e097ef3793674e57ab.tar.bz2 scala-d95eb2a8f979b9f9db98a0e097ef3793674e57ab.zip |
Made the icode reader more resilient to errors.
symbol does not cause any crashes, but the method using an unknown
symbol will not be used for inlining. Resurrected tests, removed
spec-matrix for the moment. No review.
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/Course-2002-10-msil.check | 46 | ||||
-rw-r--r-- | test/files/run/Course-2002-10.check | 46 | ||||
-rw-r--r-- | test/files/run/Course-2002-10.scala | 135 | ||||
-rw-r--r-- | test/files/run/Course-2002-13.check | 14 | ||||
-rw-r--r-- | test/files/run/Course-2002-13.scala | 323 | ||||
-rw-r--r-- | test/files/run/infiniteloop.check | 1 | ||||
-rw-r--r-- | test/files/run/infiniteloop.scala | 13 | ||||
-rw-r--r-- | test/files/run/spec-matrix.check | 1 | ||||
-rw-r--r-- | test/files/run/spec-matrix.scala | 70 | ||||
-rw-r--r-- | test/files/run/streamWithFilter.check | 5 | ||||
-rw-r--r-- | test/files/run/streamWithFilter.scala | 11 |
11 files changed, 594 insertions, 71 deletions
diff --git a/test/files/run/Course-2002-10-msil.check b/test/files/run/Course-2002-10-msil.check new file mode 100644 index 0000000000..bbd9414370 --- /dev/null +++ b/test/files/run/Course-2002-10-msil.check @@ -0,0 +1,46 @@ +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +fib(11) = 89 +fib(12) = 144 +fib(13) = 233 +fib(14) = 377 +fib(15) = 610 +fib(16) = 987 +fib(17) = 1597 +fib(18) = 2584 +fib(19) = 4181 + +pi(0) = 4 , 3.16666666666667 , 4 +pi(1) = 2.66666666666667 , 3.13333333333333 , 3.16666666666667 +pi(2) = 3.46666666666667 , 3.1452380952381 , 3.1421052631579 +pi(3) = 2.8952380952381 , 3.13968253968254 , 3.141599357319 +pi(4) = 3.33968253968254 , 3.14271284271284 , 3.14159271403378 +pi(5) = 2.97604617604618 , 3.14088134088134 , 3.14159265397529 +pi(6) = 3.28373848373848 , 3.14207181707182 , 3.14159265359118 +pi(7) = 3.01707181707182 , 3.14125482360776 , 3.14159265358978 +pi(8) = 3.25236593471888 , 3.1418396189294 , 3.14159265358979 +pi(9) = 3.0418396189294 , 3.1414067184965 , 3.14159265358979 +pi = 3.14159265358979 , 3.14159265358979 , 3.14159265358979 + +ln(0) = 1 , 0.7 , 1 +ln(1) = 0.5 , 0.69047619047619 , 0.7 +ln(2) = 0.833333333333333 , 0.694444444444444 , 0.69327731092437 +ln(3) = 0.583333333333333 , 0.692424242424242 , 0.693148869332925 +ln(4) = 0.783333333333333 , 0.693589743589744 , 0.693147196073549 +ln(5) = 0.616666666666667 , 0.692857142857143 , 0.693147180663564 +ln(6) = 0.759523809523809 , 0.693347338935574 , 0.693147180560404 +ln(7) = 0.634523809523809 , 0.693003341687552 , 0.693147180559944 +ln(8) = 0.745634920634921 , 0.693253968253968 , 0.693147180559943 +ln(9) = 0.645634920634921 , 0.693065750674446 , 0.693147180559945 +ln = 0.693147180559945 , 0.693147180559945 , 0.693147180559945 + +prime numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 diff --git a/test/files/run/Course-2002-10.check b/test/files/run/Course-2002-10.check new file mode 100644 index 0000000000..207b671f05 --- /dev/null +++ b/test/files/run/Course-2002-10.check @@ -0,0 +1,46 @@ +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +fib(11) = 89 +fib(12) = 144 +fib(13) = 233 +fib(14) = 377 +fib(15) = 610 +fib(16) = 987 +fib(17) = 1597 +fib(18) = 2584 +fib(19) = 4181 + +pi(0) = 4.0 , 3.166666666666667 , 4.0 +pi(1) = 2.666666666666667 , 3.1333333333333337, 3.166666666666667 +pi(2) = 3.466666666666667 , 3.1452380952380956, 3.142105263157895 +pi(3) = 2.8952380952380956, 3.1396825396825396, 3.1415993573190044 +pi(4) = 3.33968253968254 , 3.142712842712843 , 3.141592714033778 +pi(5) = 2.976046176046176 , 3.140881340881341 , 3.1415926539752923 +pi(6) = 3.283738483738484 , 3.142071817071817 , 3.141592653591176 +pi(7) = 3.017071817071817 , 3.1412548236077646, 3.141592653589777 +pi(8) = 3.252365934718876 , 3.1418396189294024, 3.141592653589794 +pi(9) = 3.0418396189294024, 3.141406718496502 , 3.1415926535897936 +pi = 3.141592653589793 , 3.141592653589793 , 3.141592653589793 + +ln(0) = 1.0 , 0.7 , 1.0 +ln(1) = 0.5 , 0.6904761904761905, 0.7 +ln(2) = 0.8333333333333333, 0.6944444444444444, 0.6932773109243697 +ln(3) = 0.5833333333333333, 0.6924242424242424, 0.6931488693329254 +ln(4) = 0.7833333333333333, 0.6935897435897436, 0.6931471960735491 +ln(5) = 0.6166666666666667, 0.6928571428571428, 0.6931471806635636 +ln(6) = 0.7595238095238095, 0.6933473389355742, 0.6931471805604038 +ln(7) = 0.6345238095238095, 0.6930033416875522, 0.6931471805599444 +ln(8) = 0.7456349206349207, 0.6932539682539682, 0.6931471805599426 +ln(9) = 0.6456349206349206, 0.6930657506744463, 0.6931471805599453 +ln = 0.6931471805599453, 0.6931471805599453, 0.6931471805599453 + +prime numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 diff --git a/test/files/run/Course-2002-10.scala b/test/files/run/Course-2002-10.scala new file mode 100644 index 0000000000..e978bc8258 --- /dev/null +++ b/test/files/run/Course-2002-10.scala @@ -0,0 +1,135 @@ +//############################################################################ +// Programmation IV - 2002 - Week 10 +//############################################################################ + +import Math.{Pi, log} + +object M0 { + + def addStream (s1: Stream[Int], s2: Stream[Int]): Stream[Int] = + Stream.cons(s1.head + s2.head, addStream(s1.tail, s2.tail)); + + val fib: Stream[Int] = + Stream.cons(0, Stream.cons(1, addStream(this.fib, this.fib.tail))); + + def test = { + var i = 0; + fib.take(20).foreach(n => {Console.println("fib("+i+") = "+n); i=i+1}); + Console.println; + } +} + +//############################################################################ + +object M1 { + + def scale(x: Double, s: Stream[Double]): Stream[Double] = + s map { e: Double => e*x } + + def partialSums(s: Stream[Double]): Stream[Double] = + Stream.cons(s.head, partialSums(s.tail) map (x => x + s.head)); + + def euler(s: Stream[Double]): Stream[Double] = { + val nm1 = s apply 0; + val n = s apply 1; + val np1 = s apply 2; + Stream.cons(np1 - ((np1 - n)*(np1 - n) / (nm1 - 2*n + np1)),euler(s.tail)) + }; + + def better(s: Stream[Double], transform: Stream[Double] => Stream[Double]) + : Stream[Stream[Double]] = + Stream.cons(s, better(transform(s), transform)); + + def veryGood(s: Stream[Double], transform: Stream[Double] => Stream[Double]) + : Stream[Double] = + better(s, transform) map (x => x.head); + + def lnSummands(n: Double): Stream[Double] = + Stream.cons(1.0 / n, lnSummands(n + 1.0) map { x: Double => -x }) + + var ln0 = partialSums(lnSummands(1.0)); + var ln1 = euler(ln0); + var ln2 = veryGood(ln0, euler); + + def piSummands(n: Double): Stream[Double] = + Stream.cons(1.0 / n, piSummands(n + 2.0) map { x: Double => -x }) + + var pi0 = scale(4.0, partialSums(piSummands(1.0))); + var pi1 = euler(pi0); + var pi2 = veryGood(pi0, euler); + + def pad(s: String, n: Int): String = + if (n <= 0) s.substring(0, s.length() + n) + else pad(s + " ", n - 1); + def str(d: Double) = { val s = d.toString(); pad(s, 18 - s.length()) }; + + def test = { + var i = 0; + while (i < 10) { + Console.print("pi("+i+") = "); + Console.print(str(pi0.apply(i)) + ", "); + Console.print(str(pi1.apply(i)) + ", "); + Console.print(str(pi2.apply(i)) + "\n"); + i = i + 1; + } + Console.print("pi = "); + Console.print(str(Pi) + ", "); + Console.print(str(Pi) + ", "); + Console.print(str(Pi) + "\n"); + Console.println; + i = 0; + while (i < 10) { + Console.print("ln("+i+") = "); + Console.print(str(ln0.apply(i)) + ", "); + Console.print(str(ln1.apply(i)) + ", "); + Console.print(str(ln2.apply(i)) + "\n"); + i = i + 1; + } + Console.print("ln = "); + Console.print(str(log(2)) + ", "); + Console.print(str(log(2)) + ", "); + Console.print(str(log(2)) + "\n"); + Console.println; + } +} + +//############################################################################ + +object M2 { + + class IntIterator(start: Int) extends Iterator[Int] { + var current: Int = start; + def hasNext = true; + def next = { current = current + 1; current - 1 }; + } + + class PrimeIterator() extends Iterator[Int] { + var current: Iterator[Int] = new IntIterator(2); + def hasNext = true; + def next = { + val p = current.next; + current = current filter { x => !((x % p) == 0) }; + p + } + } + + def test = { + val i = (new PrimeIterator()).take(30); + Console.print("prime numbers:"); + while (i.hasNext) { Console.print(" " + i.next); } + Console.println; + } +} + +//############################################################################ + +object Test { + def main(args: Array[String]): Unit = { + M0.test; + M1.test; + M2.test; + () + } +} + +//############################################################################ diff --git a/test/files/run/Course-2002-13.check b/test/files/run/Course-2002-13.check new file mode 100644 index 0000000000..7664f70576 --- /dev/null +++ b/test/files/run/Course-2002-13.check @@ -0,0 +1,14 @@ +List(S = jean, V = mange, A = le, D = grand, N = table) +List(S = jean, V = mange, A = le, D = grand, N = cheval) + +List(S = jean, V = mange, A = le, D = grand, N = cheval) +List(S = jean, V = mange, A = la, D = belle, N = table) + +List(S = jean, V = mange, A = le, D = nil, N = cheval) +List(S = jean, V = mange, A = le, D = cons(grand,nil), N = cheval) +List(S = jean, V = mange, A = le, D = cons(grand,cons(grand,nil)), N = cheval) +List(S = jean, V = mange, A = la, D = nil, N = table) +yes +yes +no + diff --git a/test/files/run/Course-2002-13.scala b/test/files/run/Course-2002-13.scala new file mode 100644 index 0000000000..c016d41a90 --- /dev/null +++ b/test/files/run/Course-2002-13.scala @@ -0,0 +1,323 @@ +//############################################################################ +// Programmation IV - 2002 - Week 13 +//############################################################################ + +class Tokenizer(s: String, delimiters: String) extends Iterator[String] { + + private var i = 0; + + def isDelimiter(ch: Char) = { + var i = 0; + while (i < delimiters.length() && delimiters.charAt(i) != ch) { i = i + 1 } + i < delimiters.length() + } + + def hasNext: Boolean = { + while (i < s.length() && s.charAt(i) <= ' ') { i = i + 1 } + i < s.length() + } + + def next: String = + if (hasNext) { + val start = i; + var ch = s.charAt(i); i = i + 1; + if (isDelimiter(ch)) ch.toString() + else { + while (i < s.length() && + s.charAt(i) > ' ' && + !isDelimiter(s.charAt(i))){ i = i + 1 } + s.substring(start, i) + } + } else ""; + +} + +object Terms { + + val debug = false; + + trait Term { + def map(s: Subst): Term; + def tyvars: List[String]; + } + + case class Binding(name: String, term: Term) { + term match { case Var(n) if (name == n) => error("bad binding") case _ => () } + override def toString() = name + " = " + term; + } + + type Subst = List[Binding]; + + def lookup(s: Subst, name: String): Option[Term] = s match { + case List() => None + case b :: s1 => if (name == b.name) Some(b.term) else lookup(s1, name) + } + + case class Var(a: String) extends Term { + override def toString() = a; + def map(s: Subst): Term = lookup(s, a) match { + case Some(b) => b map s + case None => this; + } + def tyvars = List(a); + } + + case class Con(a: String, ts: List[Term]) extends Term { + override def toString() = + a + (if (ts.isEmpty) "" else ts.mkString("(", ",", ")")); + def map(s: Subst): Term = Con(a, ts map (t => t map s)); + def tyvars = (ts flatMap (t => t.tyvars)).distinct; + } + + private var count = 0; + def newVar(prefix: String) = { count = count + 1; Var(prefix + count) } + + val NoTerm = Con("<none>", List()); + + def unify1(x: Term, y: Term, s: Subst): Option[Subst] = Pair(x, y) match { + case Pair(Var(a), Var(b)) if (a == b) => + Some(s) + case Pair(Var(a), _) => lookup(s, a) match { + case Some(x1) => unify(x1, y, s) + case None => if (y.tyvars contains a) None else Some(Binding(a, y) :: s) + } + case Pair(_, Var(b)) => lookup(s, b) match { + case Some(y1) => unify(x, y1, s) + case None => if (x.tyvars contains b) None else Some(Binding(b, x) :: s) + } + case Pair(Con(a, xs), Con(b, ys)) if (a == b) => + unify(xs, ys, s) + case _ => None + } + + def unify(x: Term, y: Term, s: Subst): Option[Subst] = { + val ss = unify1(x, y, s); + if (debug) Console.println("unify " + x + " with " + y + " = " + ss); + ss + } + + def unify(xs: List[Term], ys: List[Term], s: Subst): Option[Subst] = Pair(xs, ys) match { + case Pair(List(), List()) => Some(s) + case Pair(x :: xs1, y :: ys1) => + unify(x, y, s) match { + case Some(s1) => unify(xs1, ys1, s1) + case None => None + } + case _ => None + } +} + +import Terms._; + +object Programs { + + case class Clause(lhs: Term, rhs: List[Term]) { + def tyvars = + (lhs.tyvars ::: (rhs flatMap (t => t.tyvars))).distinct; + def newInstance = { + var s: Subst = List(); + for (val a <- tyvars) { s = Binding(a, newVar(a)) :: s } + Clause(lhs map s, rhs map (t => t map s)) + } + override def toString() = + lhs.toString() + " :- " + rhs.mkString("", ",", "") + "."; + } + + def list2stream[a](xs: List[a]): Stream[a] = xs match { + case List() => Stream.empty + case x :: xs1 => Stream.cons(x, list2stream(xs1)) + } + def option2stream[a](xo: Option[a]): Stream[a] = xo match { + case None => Stream.empty + case Some(x) => Stream.cons(x, Stream.empty) + } + + def solve(query: List[Term], clauses: List[Clause]): Stream[Subst] = { + + def solve2(query: List[Term], s: Subst): Stream[Subst] = query match { + case List() => + Stream.cons(s, Stream.empty) + case Con("not", qs) :: query1 => + if (solve1(qs, s).isEmpty) Stream.cons(s, Stream.empty) + else Stream.empty + case q :: query1 => + for (val clause <- list2stream(clauses); + val s1 <- tryClause(clause.newInstance, q, s); + val s2 <- solve1(query1, s1)) yield s2 + } + + def solve1(query: List[Term], s: Subst): Stream[Subst] = { + val ss = solve2(query, s); + if (debug) Console.println("solved " + query + " = " + ss); + ss + } + + def tryClause(c: Clause, q: Term, s: Subst): Stream[Subst] = { + if (debug) Console.println("trying " + c); + for (val s1 <- option2stream(unify(q, c.lhs, s)); + val s2 <- solve1(c.rhs, s1)) yield s2; + } + + solve1(query, List()) + } +} + +import Programs._; + +class Parser(s: String) { + val it = new Tokenizer(s, "(),.?"); + + var token: String = it.next; + + def syntaxError(msg: String): Unit = error(msg + ", but " + token + " found"); + + def rep[a](p: => a): List[a] = { + val t = p; + if (token == ",") { token = it.next; t :: rep(p) } else List(t) + } + + def constructor: Term = { + val a = token; + token = it.next; + Con(a, + if (token equals "(") { + token = it.next; + val ts: List[Term] = if (token equals ")") List() else rep(term); + if (token equals ")") token = it.next else syntaxError("`)' expected"); + ts + } else List()) + } + + def term: Term = { + val ch = token.charAt(0); + if ('A' <= ch && ch <= 'Z') { val a = token; token = it.next; Var(a) } + else if (it.isDelimiter(ch)) { syntaxError("term expected"); null } + else constructor + } + + def line: Clause = { + val result = + if (token equals "?") { + token = it.next; + Clause(NoTerm, rep(constructor)); + } else { + Clause( + constructor, + if (token equals ":-") { token = it.next; rep(constructor) } else List()) + } + if (token equals ".") token = it.next else syntaxError("`.' expected"); + result + } + + def all: List[Clause] = if (token equals "") List() else line :: all; +} + +object Prolog { + + def processor: String => Unit = { + var program: List[Clause] = List(); + var solutions: Stream[Subst] = Stream.empty; + var tvs: List[String] = List(); + { input => + new Parser(input).all foreach { c => + if (c.lhs == NoTerm) { + c.rhs match { + case List(Con("more", List())) => + solutions = solutions.tail; + case _ => + solutions = solve(c.rhs, program); + tvs = c.tyvars; + } + if (solutions.isEmpty) { + Console.println("no") + } else { + val s: Subst = solutions.head + .filter(b => tvs contains b.name) + .map(b => Binding(b.name, b.term map solutions.head)) + .reverse; + if (s.isEmpty) Console.println("yes") + else Console.println(s); + } + } else { + program = program ::: List(c); + } + } + } + } + + def process(code: String) = processor(code); +} + +//############################################################################ + +object Test { + def main(args: Array[String]): Unit = { + Prolog.process( + "sujet(jean).\n" + + "sujet(marie).\n" + + "verbe(mange).\n" + + "verbe(dort).\n" + + "article(le).\n" + + "article(la).\n" + + "adjectif(grand).\n" + + "adjectif(belle).\n" + + "nom(table).\n" + + "nom(cheval).\n" + + + "complement(A,D,N) :- article(A), adjectif(D), nom(N).\n" + + "phrase(S,V,A,D,N) :- sujet(S), verbe(V), complement(A,D,N).\n" + + + "?phrase(S,V,A,D,N).\n" + "?more.\n" + ); + Console.println; + + Prolog.process( + "sujet(jean).\n" + + "sujet(marie).\n" + + "verbe(mange).\n" + + "verbe(dort).\n" + + "article(le,m).\n" + + "article(la,f).\n" + + "adjectif(grand,m).\n" + + "adjectif(belle,f).\n" + + "nom(table,f).\n" + + "nom(cheval,m).\n" + + + "complement(A,D,N) :- article(A,G), adjectif(D,G), nom(N,G).\n" + + "phrase(S,V,A,D,N) :- sujet(S), verbe(V), complement(A,D,N).\n" + + + "?phrase(S,V,A,D,N).\n" + "?more.\n" + ); + Console.println; + + Prolog.process( + "sujet(jean).\n" + + "sujet(marie).\n" + + "verbe(mange).\n" + + "verbe(dort).\n" + + "article(le,m).\n" + + "article(la,f).\n" + + "adjectif(grand,m).\n" + + "adjectif(belle,f).\n" + + "nom(table,f).\n" + + "nom(cheval,m).\n" + + + "adjectifs(nil,G).\n" + + "adjectifs(cons(A1,nil),G) :- adjectif(A1,G).\n" + + "adjectifs(cons(A1,cons(A2,nil)),G) :- adjectif(A1,G),adjectif(A2,G).\n"+ + "complement(A,D,N) :- article(A,G), adjectifs(D,G), nom(N,G).\n" + + "phrase(S,V,A,D,N) :- sujet(S), verbe(V), complement(A,D,N).\n" + + + "?phrase(S,V,A,D,N).\n" + "?more.\n" + "?more.\n" + "?more.\n" + + + "?phrase(jean,mange,le,nil,cheval).\n" + + "?phrase(jean,mange,le,cons(grand,nil),cheval).\n" + + "?phrase(jean,mange,le,cons(grand,nil),table).\n" + ); + Console.println; + + () + } +} + +//############################################################################ diff --git a/test/files/run/infiniteloop.check b/test/files/run/infiniteloop.check new file mode 100644 index 0000000000..6f8cf6e4d9 --- /dev/null +++ b/test/files/run/infiniteloop.check @@ -0,0 +1 @@ +Stream(512, 256, 128, 64, 32, 16, 8, 4, 2, 1) diff --git a/test/files/run/infiniteloop.scala b/test/files/run/infiniteloop.scala new file mode 100644 index 0000000000..f15674a676 --- /dev/null +++ b/test/files/run/infiniteloop.scala @@ -0,0 +1,13 @@ +/** Tests the optimiser (not to loop on 'reverse'). */ + +object Test extends Application { + def foo { + val s3 = Stream.range(1, 1000) //100000 (ticket #153: Stackoverflow) + + // ticket #153 + def powers(x: Int) = if ((x&(x-1)) == 0) Some(x) else None + println(s3.flatMap(powers).reverse) + } + + foo +} diff --git a/test/files/run/spec-matrix.check b/test/files/run/spec-matrix.check deleted file mode 100644 index 72e8ffc0db..0000000000 --- a/test/files/run/spec-matrix.check +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/test/files/run/spec-matrix.scala b/test/files/run/spec-matrix.scala deleted file mode 100644 index 81e3eaf212..0000000000 --- a/test/files/run/spec-matrix.scala +++ /dev/null @@ -1,70 +0,0 @@ -/** Test matrix multiplication with specialization. - */ - -class Matrix[@specialized A: ClassManifest](val rows: Int, val cols: Int) { - private val arr: Array[Array[A]] = new Array[Array[A]](rows, cols) - - def apply(i: Int, j: Int): A = { - if (i < 0 || i >= rows || j < 0 || j >= cols) - throw new NoSuchElementException("Indexes out of bounds: " + (i, j)) - - arr(i)(j) - } - - def update(i: Int, j: Int, e: A) { - arr(i)(j) = e - } - - def rowsIterator: Iterator[Array[A]] = new Iterator[Array[A]] { - var idx = 0; - def hasNext = idx < rows - def next = { - idx += 1 - arr(idx - 1) - } - } -} - -object Test { - def main(args: Array[String]) { - val m = randomMatrix(200, 100) - val n = randomMatrix(100, 200) - - mult(m, n) - println("*") - } - - def randomMatrix(n: Int, m: Int) = { - val r = new util.Random(10) - val x = new Matrix[Int](n, m) - for (i <- 0 until n; j <- 0 until m) - x(i, j) = r.nextInt - x - } - - - def multManifest[@specialized(Int) T](m: Matrix[T], n: Matrix[T])(implicit cm: ClassManifest[T], num: Numeric[T]) { - val p = new Matrix[T](m.rows, n.cols) - import num._ - - for (i <- 0 until m.rows) - for (j <- 0 until n.cols) { - var sum = num.zero - for (k <- 0 until n.rows) - sum += m(i, k) * n(k, j) - p(i, j) = sum - } - } - - def mult(m: Matrix[Int], n: Matrix[Int]) { - val p = new Matrix[Int](m.rows, n.cols) - - for (i <- 0 until m.rows) - for (j <- 0 until n.cols) { - var sum = 0 - for (k <- 0 until n.rows) - sum += m(i, k) * n(k, j) - p(i, j) = sum - } - } -} diff --git a/test/files/run/streamWithFilter.check b/test/files/run/streamWithFilter.check new file mode 100644 index 0000000000..6b0e91a147 --- /dev/null +++ b/test/files/run/streamWithFilter.check @@ -0,0 +1,5 @@ +15 +30 +45 +60 +75 diff --git a/test/files/run/streamWithFilter.scala b/test/files/run/streamWithFilter.scala new file mode 100644 index 0000000000..cb919d4f55 --- /dev/null +++ b/test/files/run/streamWithFilter.scala @@ -0,0 +1,11 @@ +object Test { + val nums = Stream.from(1) + def isFizz(x: Int) = x % 3 == 0 + def isBuzz(x: Int) = x % 5 == 0 + // next line will run forever if withFilter isn't doing its thing. + val fizzbuzzes = for (n <- nums ; if isFizz(n) ; if isBuzz(n)) yield n + + def main(args: Array[String]): Unit = { + fizzbuzzes take 5 foreach println + } +} |