Skip to content

Commit

Permalink
Don't evaluate suffix twice in LazyList#lazyAppendedAll
Browse files Browse the repository at this point in the history
Port of scala/scala#8851.

Co-authored-by: NthPortal <nthportal@gmail.com>
  • Loading branch information
ijuma and NthPortal committed Jul 5, 2020
1 parent 223bdd9 commit b595f47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ final class LazyList[+A] private (private[this] var lazyState: () => LazyList.St
newLL {
if (isEmpty) suffix match {
case lazyList: LazyList[B] => lazyList.state // don't recompute the LazyList
case _ => stateFromIterator(suffix.toIterator)
case coll => stateFromIterator(coll.toIterator)
} else sCons(head, tail lazyAppendedAll suffix)
}

Expand Down
10 changes: 9 additions & 1 deletion compat/src/test/scala/test/scala/collection/LazyListTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class LazyListTest {
assertEquals(1 to 10, build(_ ++= LazyList.from(1).take(10)))
assertEquals(1 to 10, build(_ ++= Iterator.from(1).take(10)))
}

@Test
def selfReferentialFailure(): Unit = {
def assertNoStackOverflow[A](lazyList: LazyList[A]): Unit = {
Expand All @@ -372,4 +372,12 @@ class LazyListTest {
assertNoStackOverflow((new L).a)
assertNoStackOverflow((new L).b)
}

// scala/bug#11931
@Test
def lazyAppendedAllExecutesOnce(): Unit = {
var count = 0
LazyList(1).lazyAppendedAll({ count += 1; Seq(2)}).toList
assertEquals(1, count)
}
}

0 comments on commit b595f47

Please sign in to comment.