@@ -631,6 +631,7 @@ class IteratorTest {
631
631
632
632
@ Test def `flatMap is memory efficient in previous element` (): Unit = {
633
633
import java .lang .ref ._
634
+ import scala .util .chaining ._
634
635
// Array.iterator holds onto array reference; by contrast, iterating over List walks tail.
635
636
// Avoid reaching seq1 through test class. Avoid testing Array.iterator.
636
637
class C extends Iterable [String ] {
@@ -641,11 +642,7 @@ class IteratorTest {
641
642
642
643
def hasNext = i < ss.length
643
644
644
- def next () =
645
- if (hasNext) {
646
- val res = ss(i); i += 1 ; res
647
- }
648
- else Iterator .empty.next()
645
+ def next () = if (hasNext) ss(i).tap(_ => i += 1 ) else Iterator .empty.next()
649
646
}
650
647
651
648
def apply (i : Int ) = ss(i)
@@ -654,13 +651,13 @@ class IteratorTest {
654
651
val seq2 = List (" third" )
655
652
val it0 : Iterator [Int ] = Iterator (1 , 2 )
656
653
lazy val it : Iterator [String ] = it0.flatMap {
657
- case 1 => seq1.get
654
+ case 1 => Option ( seq1.get).getOrElse( Nil )
658
655
case _ => check(); seq2
659
656
}
660
657
661
658
def check () = assertNotReachable(seq1.get, it)(())
662
659
663
- def checkHasElement () = assertNotReachable(seq1.get. apply(1 ), it)(())
660
+ def checkHasElement () = assertNotReachable(Option ( seq1.get).map(_. apply(1 )).orNull , it)(())
664
661
665
662
assert(it.hasNext)
666
663
assertEquals(" first" , it.next())
0 commit comments