Skip to content

Commit 76ab5b6

Browse files
ijumaNthPortal
andcommitted
Make LazyList.cons.apply lazier
Port of scala/scala#9095. Co-authored-by: NthPortal <[email protected]>
1 parent 85096c3 commit 76ab5b6

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ object LazyList extends SeqFactory[LazyList] {
12641264
* @param hd The first element of the result lazy list
12651265
* @param tl The remaining elements of the result lazy list
12661266
*/
1267-
def apply[A](hd: => A, tl: => LazyList[A]): LazyList[A] = newLL(sCons(hd, tl))
1267+
def apply[A](hd: => A, tl: => LazyList[A]): LazyList[A] = newLL(sCons(hd, newLL(tl.state)))
12681268

12691269
/** Maps a lazy list to its head and tail */
12701270
def unapply[A](xs: LazyList[A]): Option[(A, LazyList[A])] = #::.unapply(xs)

compat/src/test/scala/test/scala/collection/LazyListLazinessTest.scala

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,20 @@ class LazyListLazinessTest {
715715
// assertLazyAllSkipping(op, 4)
716716
// }
717717

718+
private def genericCons_unapply_properlyLazy(unapply: LazyList[Int] => Option[(Int, LazyList[Int])]): Unit = {
719+
assertLazyAllSkipping(unapply, 1)
720+
}
721+
722+
@Test
723+
def cons_unapply_properlyLazy(): Unit = {
724+
genericCons_unapply_properlyLazy(LazyList.cons.unapply)
725+
}
726+
727+
@Test
728+
def `#::_unapply_properlyLazy`(): Unit = {
729+
genericCons_unapply_properlyLazy(LazyList.#::.unapply)
730+
}
731+
718732
/* factory laziness tests */
719733

720734
@Test
@@ -776,14 +790,12 @@ class LazyListLazinessTest {
776790
assertRepeatedlyLazy(factory)
777791
}
778792

779-
@Test
780-
def `#:: properlyLazy`(): Unit = {
793+
private def genericCons_properlyLazy(cons: (=> Int, => LazyList[Int]) => LazyList[Int]): Unit = {
781794
val headInitFactory = lazyListFactory { init =>
782795
def gen(index: Int): LazyList[Int] = {
783796
def elem(): Int = { init.evaluate(index); index }
784797
if (index >= LazinessChecker.count) LazyList.empty
785-
// else elem() #:: gen(index + 1)
786-
else gen(index + 1).#::(elem())
798+
else cons(elem(), gen(index + 1))
787799
}
788800

789801
gen(0)
@@ -795,7 +807,7 @@ class LazyListLazinessTest {
795807
if (index >= LazinessChecker.count) LazyList.empty
796808
else {
797809
init.evaluate(index)
798-
index #:: gen(index + 1)
810+
cons(index, gen(index + 1))
799811
}
800812
}
801813

@@ -804,6 +816,16 @@ class LazyListLazinessTest {
804816
assertRepeatedlyLazy(tailInitFactory)
805817
}
806818

819+
@Test
820+
def cons_properlyLazy(): Unit = {
821+
genericCons_properlyLazy(LazyList.cons(_, _))
822+
}
823+
824+
@Test
825+
def `#::_properlyLazy`(): Unit = {
826+
genericCons_properlyLazy(_ #:: _)
827+
}
828+
807829
@Test
808830
def `#::: properlyLazy`(): Unit = {
809831
val headInitFactory = lazyListFactory { init =>

0 commit comments

Comments
 (0)