Skip to content

Commit 9a1b416

Browse files
ijumaNthPortal
andcommitted
Don't evaluate suffix twice in LazyList#lazyAppendedAll
Port of scala/scala#8851. Co-authored-by: NthPortal <[email protected]>
1 parent b2ccb28 commit 9a1b416

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ final class LazyList[+A] private (private[this] var lazyState: () => LazyList.St
365365
newLL {
366366
if (isEmpty) suffix match {
367367
case lazyList: LazyList[B] => lazyList.state // don't recompute the LazyList
368-
case _ => stateFromIterator(suffix.toIterator)
368+
case coll => stateFromIterator(coll.toIterator)
369369
} else sCons(head, tail lazyAppendedAll suffix)
370370
}
371371

compat/src/test/scala/test/scala/collection/LazyListTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,12 @@ class LazyListTest {
376376
assertNoStackOverflow((new L).a)
377377
assertNoStackOverflow((new L).b)
378378
}
379+
380+
// scala/bug#11931
381+
@Test
382+
def lazyAppendedAllExecutesOnce(): Unit = {
383+
var count = 0
384+
LazyList(1).lazyAppendedAll({ count += 1; Seq(2)}).toList
385+
assertEquals(1, count)
386+
}
379387
}

0 commit comments

Comments
 (0)