Skip to content

Commit

Permalink
Merge pull request #8985 from NthPortal/topic/ll-cons-laziness/PR
Browse files Browse the repository at this point in the history
Increase laziness of #:: for LazyList
  • Loading branch information
SethTisue committed May 20, 2020
2 parents 80aff40 + 905eabc commit a8a7261
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/LazyList.scala
Expand Up @@ -1117,7 +1117,7 @@ object LazyList extends SeqFactory[LazyList] {
/** Construct a LazyList consisting of a given first element followed by elements
* from another LazyList.
*/
def #:: [B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, l()))
def #:: [B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, newLL(l().state)))
/** Construct a LazyList consisting of the concatenation of the given LazyList and
* another LazyList.
*/
Expand Down
36 changes: 31 additions & 5 deletions test/junit/scala/collection/immutable/LazyListLazinessTest.scala
Expand Up @@ -205,7 +205,7 @@ class LazyListLazinessTest {
}

@Test
def lazyAppendedAll_appendedAll_properlyLazy(): Unit = {
def lazyAppendedAll_properlyLazy(): Unit = {
genericAppendedColl_properlyLazy(_ lazyAppendedAll _)
}

Expand Down Expand Up @@ -748,7 +748,7 @@ class LazyListLazinessTest {

@Test
def `#:: properlyLazy`(): Unit = {
val factory = lazyListFactory { init =>
val headInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
def elem(): Int = { init.evaluate(index); index }
if (index >= LazinessChecker.count) LazyList.empty
Expand All @@ -757,12 +757,25 @@ class LazyListLazinessTest {

gen(0)
}
assertRepeatedlyLazy(factory)
assertRepeatedlyLazy(headInitFactory)

val tailInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
if (index >= LazinessChecker.count) LazyList.empty
else {
init.evaluate(index)
index #:: gen(index + 1)
}
}

LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
}
assertRepeatedlyLazy(tailInitFactory)
}

@Test
def `#::: properlyLazy`(): Unit = {
val factory = lazyListFactory { init =>
val headInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
def elem(): LazyList[Int] = LazyList.fill(1) { init.evaluate(index); index }
if (index >= LazinessChecker.count) LazyList.empty
Expand All @@ -771,7 +784,20 @@ class LazyListLazinessTest {

gen(0)
}
assertRepeatedlyLazy(factory)
assertRepeatedlyLazy(headInitFactory)

val tailInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
if (index >= LazinessChecker.count) LazyList.empty
else {
init.evaluate(index)
LazyList.fill(1)(index) #::: gen(index + 1)
}
}

LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
}
assertRepeatedlyLazy(tailInitFactory)
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion test/junit/scala/collection/immutable/LazyListTest.scala
Expand Up @@ -163,7 +163,7 @@ class LazyListTest {
cyc.tail.tail.head
assertEquals("LazyList(1, 2, 3, <not computed>)", cyc.toString)
cyc.tail.tail.tail.head
assertEquals("LazyList(1, 2, 3, 4, <cycle>)", cyc.toString)
assertEquals("LazyList(1, 2, 3, 4, <not computed>)", cyc.toString)
cyc.tail.tail.tail.tail.head
assertEquals("LazyList(1, 2, 3, 4, <cycle>)", cyc.toString)
}
Expand Down

0 comments on commit a8a7261

Please sign in to comment.