Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase laziness of #:: for LazyList #8985

Merged
merged 1 commit into from May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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)
julienrf marked this conversation as resolved.
Show resolved Hide resolved
cyc.tail.tail.tail.tail.head
assertEquals("LazyList(1, 2, 3, 4, <cycle>)", cyc.toString)
}
Expand Down