diff options
author | michelou <michelou@epfl.ch> | 2007-12-12 18:21:14 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-12-12 18:21:14 +0000 |
commit | bd42999939b338728a3c96bcfa74166f51a8b341 (patch) | |
tree | 97bf1a2526d4771c1728418e7fb36e1cf264156a | |
parent | 98e9995ddd3b555aeca73f82671f86107a9ea68f (diff) | |
download | scala-bd42999939b338728a3c96bcfa74166f51a8b341.tar.gz scala-bd42999939b338728a3c96bcfa74166f51a8b341.tar.bz2 scala-bd42999939b338728a3c96bcfa74166f51a8b341.zip |
Fixed #153 (thanks to Sean)
-rw-r--r-- | src/library/scala/Stream.scala | 37 | ||||
-rw-r--r-- | test/files/run/streams.check | 4 | ||||
-rw-r--r-- | test/files/run/streams.scala | 2 |
3 files changed, 24 insertions, 19 deletions
diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index a771e1d217..1415e3ad06 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -58,7 +58,7 @@ object Stream { } } - def unapply[A](str: Stream[A]): Option[(A,Stream[A])] = + def unapply[A](str: Stream[A]): Option[(A, Stream[A])] = if(str.isEmpty) None else @@ -113,7 +113,7 @@ object Stream { def range(start: Int, end: Int, step: Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = if (lo >= end) empty - else cons(lo, loop(lo + step)); + else cons(lo, loop(lo + step)) loop(start) } @@ -131,7 +131,7 @@ object Stream { def range(start: Int, end: Int, step: Int => Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = if (lo >= end) empty - else cons(lo, loop(step(lo))); + else cons(lo, loop(step(lo))) loop(start) } @@ -170,7 +170,7 @@ object Stream { * @return the stream composed of n elements all equal to elem */ def make[A](n: Int, elem: A): Stream[A] = - Stream.const(elem).take(n) + Stream.const(elem) take n } /** @@ -237,7 +237,7 @@ trait Stream[+A] extends Seq.Projection[A] { * @param rest The stream that gets appended to this stream */ override def append[B >: A](rest: => Iterable[B]): Stream[B] = - if (isEmpty) rest.toStream else Stream.cons(head, tail.append(rest)) + if (isEmpty) rest.toStream else Stream.cons(head, tail append rest) /** An iterator returning the elements of this stream one by one. @@ -288,7 +288,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def take(n: Int): Stream[A] = if (isEmpty || n <= 0) Stream.empty - else Stream.cons(head, tail.take(n-1)) + else Stream.cons(head, tail take (n-1)) /** Returns the stream without its <code>n</code> first elements. * If the stream has less than <code>n</code> elements, the empty stream is returned. @@ -313,7 +313,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def takeWhile(p: A => Boolean): Stream[A] = if (isEmpty || !p(head)) Stream.empty - else Stream.cons(head, tail.takeWhile(p)) + else Stream.cons(head, tail takeWhile p) /** Returns the longest suffix of this stream whose first element * does not satisfy the predicate <code>p</code>. @@ -337,7 +337,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def map[B](f: A => B): Stream[B] = if (isEmpty) Stream.empty - else Stream.cons(f(head), tail.map(f)) + else Stream.cons(f(head), tail map f) /** Apply the given function <code>f</code> to each element of this stream * (while respecting the order of the elements). @@ -374,11 +374,10 @@ trait Stream[+A] extends Seq.Projection[A] { * predicate <code>p</code>. */ override def forall(p: A => Boolean): Boolean = { - def loop(s: Stream[A]): Boolean = { + def loop(s: Stream[A]): Boolean = if (s.isEmpty) true else if (p(s.head)) loop(s.tail) else false - } loop(this) } @@ -390,11 +389,10 @@ trait Stream[+A] extends Seq.Projection[A] { * satisfies the predicate <code>p</code>. */ override def exists(p: A => Boolean): Boolean = { - def loop(s: Stream[A]): Boolean = { + def loop(s: Stream[A]): Boolean = if (s.isEmpty) false else if (p(s.head)) true else loop(s.tail) - } loop(this) } @@ -433,7 +431,14 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def flatMap[B](f: A => Iterable[B]): Stream[B] = if (isEmpty) Stream.empty - else Stream.fromIterator(f(head).elements) append (tail flatMap f) + else { + val s: Stream[B] = f(head) match { + case x: Stream[_] => x + case y: List[_] => y.toStream + case z => z.toList.toStream + } + s append (tail flatMap f) + } override def toStream = this @@ -466,9 +471,9 @@ trait Stream[+A] extends Seq.Projection[A] { * <code>Stream(a<sub>0</sub>, ..., a<sub>m</sub>) * zip Stream(b<sub>0</sub>, ..., b<sub>n</sub>)</code> is invoked. */ - def zip[B](that: Stream[B]): Stream[Tuple2[A, B]] = + def zip[B](that: Stream[B]): Stream[(A, B)] = if (this.isEmpty || that.isEmpty) Stream.empty - else Stream.cons(Tuple2(this.head, that.head), this.tail.zip(that.tail)) + else Stream.cons((this.head, that.head), this.tail zip that.tail) /** Returns a stream that pairs each element of this stream @@ -477,7 +482,7 @@ trait Stream[+A] extends Seq.Projection[A] { * @return the stream <code>Stream({a<sub>0</sub>,0}, {a<sub>0</sub>,1},...)</code> * where <code>a<sub>i</sub></code> are the elements of this stream. */ - def zipWithIndex: Stream[Tuple2[A, Int]] = + def zipWithIndex: Stream[(A, Int)] = zip(Stream.from(0)) /** Prints elements of this stream one by one, separated by commas */ diff --git a/test/files/run/streams.check b/test/files/run/streams.check index 180217df4a..f36b719ac1 100644 --- a/test/files/run/streams.check +++ b/test/files/run/streams.check @@ -15,5 +15,5 @@ Stream() Stream(1, 2) Stream() -9999 -8192 +99999 +65536 diff --git a/test/files/run/streams.scala b/test/files/run/streams.scala index a4c5e132b2..19bb9c943d 100644 --- a/test/files/run/streams.scala +++ b/test/files/run/streams.scala @@ -22,7 +22,7 @@ object Test extends Application { println(s2.dropWhile(_ > 0)) println - val s3 = Stream.range(1, 10000) // 100000 (ticket #153: Stackoverflow) + val s3 = Stream.range(1, 100000) // ticket #153: Stackoverflow println(s3.length) // ticket #153 |