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 @@ -737,6 +737,12 @@ sealed abstract class Stream[+A] extends AbstractSeq[A] with LinearSeq[A] with L
737
737
}
738
738
}
739
739
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
+
740
746
/** LazyList specialization of foldLeft which allows GC to collect along the
741
747
* way.
742
748
*
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