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 LazyList.cons.apply #9095

Merged
merged 1 commit into from Jul 2, 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 @@ -1134,7 +1134,7 @@ object LazyList extends SeqFactory[LazyList] {
* @param hd The first element of the result lazy list
* @param tl The remaining elements of the result lazy list
*/
def apply[A](hd: => A, tl: => LazyList[A]): LazyList[A] = newLL(sCons(hd, tl))
def apply[A](hd: => A, tl: => LazyList[A]): LazyList[A] = newLL(sCons(hd, newLL(tl.state)))

/** Maps a lazy list to its head and tail */
def unapply[A](xs: LazyList[A]): Option[(A, LazyList[A])] = #::.unapply(xs)
Expand Down
Expand Up @@ -693,6 +693,20 @@ class LazyListLazinessTest {
assertLazyAllSkipping(op, 4)
}

private def genericCons_unapply_properlyLazy(unapply: LazyList[Int] => Option[(Int, LazyList[Int])]): Unit = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there were no tests for these while I was in the area, so I added tests

assertLazyAllSkipping(unapply, 1)
}

@Test
def cons_unapply_properlyLazy(): Unit = {
genericCons_unapply_properlyLazy(LazyList.cons.unapply)
}

@Test
def `#::_unapply_properlyLazy`(): Unit = {
genericCons_unapply_properlyLazy(LazyList.#::.unapply)
}

/* factory laziness tests */

@Test
Expand Down Expand Up @@ -746,13 +760,12 @@ class LazyListLazinessTest {
assertRepeatedlyLazy(factory)
}

@Test
def `#:: properlyLazy`(): Unit = {
private def genericCons_properlyLazy(cons: (=> Int, => LazyList[Int]) => LazyList[Int]): Unit = {
val headInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
def elem(): Int = { init.evaluate(index); index }
if (index >= LazinessChecker.count) LazyList.empty
else elem() #:: gen(index + 1)
else cons(elem(), gen(index + 1))
}

gen(0)
Expand All @@ -764,7 +777,7 @@ class LazyListLazinessTest {
if (index >= LazinessChecker.count) LazyList.empty
else {
init.evaluate(index)
index #:: gen(index + 1)
cons(index, gen(index + 1))
}
}

Expand All @@ -774,7 +787,17 @@ class LazyListLazinessTest {
}

@Test
def `#::: properlyLazy`(): Unit = {
def cons_properlyLazy(): Unit = {
genericCons_properlyLazy(LazyList.cons(_, _))
}

@Test
def `#::_properlyLazy`(): Unit = {
genericCons_properlyLazy(_ #:: _)
}

@Test
def `#:::_properlyLazy`(): Unit = {
val headInitFactory = lazyListFactory { init =>
def gen(index: Int): LazyList[Int] = {
def elem(): LazyList[Int] = LazyList.fill(1) { init.evaluate(index); index }
Expand Down