Skip to content

Commit 85c0ee7

Browse files
committed
override Stream#take
fix scala/bug#10883
1 parent 9335974 commit 85c0ee7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/library/scala/collection/immutable/LazyList.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ sealed abstract class Stream[+A] extends AbstractSeq[A] with LinearSeq[A] with L
737737
}
738738
}
739739

740+
override def take(n: Int): Stream[A] = {
741+
if (n <= 0 || isEmpty) Stream.empty
742+
else if (n == 1) new Stream.Cons(head, Stream.empty)
743+
else new Stream.Cons(head, tail.take(n - 1))
744+
}
745+
740746
/** LazyList specialization of foldLeft which allows GC to collect along the
741747
* way.
742748
*

test/junit/scala/collection/immutable/StreamTest.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,15 @@ class StreamTest {
237237
s2.flatMap { i => (if(i < 5) None else Some(i)): Option[Int] }.headOption
238238
assertEquals(5, it1.current)
239239
}
240+
241+
@Test
242+
def t10883: Unit = {
243+
var value: Int = -1
244+
Stream.iterate(0){ a =>
245+
val next = a + 1
246+
value = next
247+
next
248+
}.take(3).toList
249+
assertEquals(value, 2)
250+
}
240251
}

0 commit comments

Comments
 (0)