Skip to content

Commit

Permalink
Increase laziness of #:: for LazyList
Browse files Browse the repository at this point in the history
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
  • Loading branch information
ijuma and NthPortal committed Aug 24, 2020
1 parent 9a1b416 commit 85096c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Expand Up @@ -1277,7 +1277,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
Expand Up @@ -217,7 +217,7 @@ class LazyListLazinessTest {
}

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

Expand Down Expand Up @@ -778,7 +778,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 @@ -788,12 +788,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 @@ -802,7 +815,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
Expand Up @@ -161,7 +161,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 85096c3

Please sign in to comment.