Skip to content

Commit 85096c3

Browse files
ijumaNthPortal
andcommitted
Increase laziness of #:: for LazyList
Port of scala/scala#8985. Co-authored-by: NthPortal <[email protected]>
1 parent 9a1b416 commit 85096c3

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
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
@@ -1277,7 +1277,7 @@ object LazyList extends SeqFactory[LazyList] {
12771277
/** Construct a LazyList consisting of a given first element followed by elements
12781278
* from another LazyList.
12791279
*/
1280-
def #::[B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, l()))
1280+
def #::[B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, newLL(l().state)))
12811281

12821282
/** Construct a LazyList consisting of the concatenation of the given LazyList and
12831283
* another LazyList.

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class LazyListLazinessTest {
217217
}
218218

219219
@Test
220-
def lazyAppendedAll_appendedAll_properlyLazy(): Unit = {
220+
def lazyAppendedAll_properlyLazy(): Unit = {
221221
genericAppendedColl_properlyLazy(_ lazyAppendedAll _)
222222
}
223223

@@ -778,7 +778,7 @@ class LazyListLazinessTest {
778778

779779
@Test
780780
def `#:: properlyLazy`(): Unit = {
781-
val factory = lazyListFactory { init =>
781+
val headInitFactory = lazyListFactory { init =>
782782
def gen(index: Int): LazyList[Int] = {
783783
def elem(): Int = { init.evaluate(index); index }
784784
if (index >= LazinessChecker.count) LazyList.empty
@@ -788,12 +788,25 @@ class LazyListLazinessTest {
788788

789789
gen(0)
790790
}
791-
assertRepeatedlyLazy(factory)
791+
assertRepeatedlyLazy(headInitFactory)
792+
793+
val tailInitFactory = lazyListFactory { init =>
794+
def gen(index: Int): LazyList[Int] = {
795+
if (index >= LazinessChecker.count) LazyList.empty
796+
else {
797+
init.evaluate(index)
798+
index #:: gen(index + 1)
799+
}
800+
}
801+
802+
LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
803+
}
804+
assertRepeatedlyLazy(tailInitFactory)
792805
}
793806

794807
@Test
795808
def `#::: properlyLazy`(): Unit = {
796-
val factory = lazyListFactory { init =>
809+
val headInitFactory = lazyListFactory { init =>
797810
def gen(index: Int): LazyList[Int] = {
798811
def elem(): LazyList[Int] = LazyList.fill(1) { init.evaluate(index); index }
799812
if (index >= LazinessChecker.count) LazyList.empty
@@ -802,7 +815,20 @@ class LazyListLazinessTest {
802815

803816
gen(0)
804817
}
805-
assertRepeatedlyLazy(factory)
818+
assertRepeatedlyLazy(headInitFactory)
819+
820+
val tailInitFactory = lazyListFactory { init =>
821+
def gen(index: Int): LazyList[Int] = {
822+
if (index >= LazinessChecker.count) LazyList.empty
823+
else {
824+
init.evaluate(index)
825+
LazyList.fill(1)(index) #::: gen(index + 1)
826+
}
827+
}
828+
829+
LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
830+
}
831+
assertRepeatedlyLazy(tailInitFactory)
806832
}
807833

808834
@Test

compat/src/test/scala/test/scala/collection/LazyListTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class LazyListTest {
161161
cyc.tail.tail.head
162162
assertEquals("LazyList(1, 2, 3, <not computed>)", cyc.toString)
163163
cyc.tail.tail.tail.head
164-
assertEquals("LazyList(1, 2, 3, 4, <cycle>)", cyc.toString)
164+
assertEquals("LazyList(1, 2, 3, 4, <not computed>)", cyc.toString)
165165
cyc.tail.tail.tail.tail.head
166166
assertEquals("LazyList(1, 2, 3, 4, <cycle>)", cyc.toString)
167167
}

0 commit comments

Comments
 (0)