File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed
src/library/scala/collection/immutable
test/junit/scala/collection/immutable Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -739,6 +739,12 @@ sealed abstract class Stream[+A] extends AbstractSeq[A] with LinearSeq[A] with L
739
739
}
740
740
}
741
741
742
+ override def take (n : Int ): Stream [A ] = {
743
+ if (n <= 0 || isEmpty) Stream .empty
744
+ else if (n == 1 ) new Stream .Cons (head, Stream .empty)
745
+ else new Stream .Cons (head, tail.take(n - 1 ))
746
+ }
747
+
742
748
/** LazyList specialization of foldLeft which allows GC to collect along the
743
749
* way.
744
750
*
Original file line number Diff line number Diff line change @@ -237,4 +237,15 @@ class StreamTest {
237
237
s2.flatMap { i => (if (i < 5 ) None else Some (i)): Option [Int ] }.headOption
238
238
assertEquals(5 , it1.current)
239
239
}
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
+ }
240
251
}
You can’t perform that action at this time.
0 commit comments