diff options
-rw-r--r-- | src/library/scala/Stream.scala | 4 | ||||
-rw-r--r-- | test/files/run/streams.check | 17 | ||||
-rw-r--r-- | test/files/run/streams.scala | 24 |
3 files changed, 43 insertions, 2 deletions
diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index afd653480c..5ad3140968 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -243,7 +243,7 @@ trait Stream[+A] extends Seq[A] { * @return the <code>n</code> first elements of this stream. */ override def take(n: Int): Stream[A] = - if (n == 0) Stream.empty + if (isEmpty || n <= 0) Stream.empty else Stream.cons(head, tail.take(n-1)) /** Returns the stream without its <code>n</code> first elements. @@ -254,7 +254,7 @@ trait Stream[+A] extends Seq[A] { */ override def drop(n: Int): Stream[A] = { def loop(s: Stream[A], n: Int): Stream[A] = - if (n == 0) s + if (s.isEmpty || n <= 0) s else loop(s.tail, n-1) loop(this, n) } diff --git a/test/files/run/streams.check b/test/files/run/streams.check new file mode 100644 index 0000000000..04d92425d9 --- /dev/null +++ b/test/files/run/streams.check @@ -0,0 +1,17 @@ +Stream() +Stream() + +Array(1) +Stream(1, ?) +Stream(1, ?) +Stream() +Stream() +Stream(1) +Stream() + +Array(1, 2) +Stream(2) +Stream() +Stream(1, 2) +Stream() + diff --git a/test/files/run/streams.scala b/test/files/run/streams.scala new file mode 100644 index 0000000000..cd1185344b --- /dev/null +++ b/test/files/run/streams.scala @@ -0,0 +1,24 @@ +object Test extends Application { + val s0: Stream[Int] = Stream.empty + println(s0.take(1)) + println(s0.takeWhile(_ > 0)) + println + + val s1 = Stream.cons(1, Stream.empty) + println(s1.toArray) + println(s1.take(1)) + println(s1.take(2)) + println(s1.drop(1)) + println(s1.drop(2)) + println(s1.drop(-1)) + println(s1.dropWhile(_ > 0)) + println + + val s2 = s1.append(Stream.cons(2, Stream.empty)) + println(s2.toArray) + println(s2.drop(1)) + println(s2.drop(2)) + println(s2.drop(-1)) + println(s2.dropWhile(_ > 0)) + println +} |