Skip to content

Commit a8a7261

Browse files
authored
Merge pull request scala#8985 from NthPortal/topic/ll-cons-laziness/PR
Increase laziness of #:: for LazyList
2 parents 80aff40 + 905eabc commit a8a7261

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/library/scala/collection/immutable/LazyList.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ object LazyList extends SeqFactory[LazyList] {
11171117
/** Construct a LazyList consisting of a given first element followed by elements
11181118
* from another LazyList.
11191119
*/
1120-
def #:: [B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, l()))
1120+
def #:: [B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, newLL(l().state)))
11211121
/** Construct a LazyList consisting of the concatenation of the given LazyList and
11221122
* another LazyList.
11231123
*/

test/junit/scala/collection/immutable/LazyListLazinessTest.scala

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class LazyListLazinessTest {
205205
}
206206

207207
@Test
208-
def lazyAppendedAll_appendedAll_properlyLazy(): Unit = {
208+
def lazyAppendedAll_properlyLazy(): Unit = {
209209
genericAppendedColl_properlyLazy(_ lazyAppendedAll _)
210210
}
211211

@@ -748,7 +748,7 @@ class LazyListLazinessTest {
748748

749749
@Test
750750
def `#:: properlyLazy`(): Unit = {
751-
val factory = lazyListFactory { init =>
751+
val headInitFactory = lazyListFactory { init =>
752752
def gen(index: Int): LazyList[Int] = {
753753
def elem(): Int = { init.evaluate(index); index }
754754
if (index >= LazinessChecker.count) LazyList.empty
@@ -757,12 +757,25 @@ class LazyListLazinessTest {
757757

758758
gen(0)
759759
}
760-
assertRepeatedlyLazy(factory)
760+
assertRepeatedlyLazy(headInitFactory)
761+
762+
val tailInitFactory = lazyListFactory { init =>
763+
def gen(index: Int): LazyList[Int] = {
764+
if (index >= LazinessChecker.count) LazyList.empty
765+
else {
766+
init.evaluate(index)
767+
index #:: gen(index + 1)
768+
}
769+
}
770+
771+
LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
772+
}
773+
assertRepeatedlyLazy(tailInitFactory)
761774
}
762775

763776
@Test
764777
def `#::: properlyLazy`(): Unit = {
765-
val factory = lazyListFactory { init =>
778+
val headInitFactory = lazyListFactory { init =>
766779
def gen(index: Int): LazyList[Int] = {
767780
def elem(): LazyList[Int] = LazyList.fill(1) { init.evaluate(index); index }
768781
if (index >= LazinessChecker.count) LazyList.empty
@@ -771,7 +784,20 @@ class LazyListLazinessTest {
771784

772785
gen(0)
773786
}
774-
assertRepeatedlyLazy(factory)
787+
assertRepeatedlyLazy(headInitFactory)
788+
789+
val tailInitFactory = lazyListFactory { init =>
790+
def gen(index: Int): LazyList[Int] = {
791+
if (index >= LazinessChecker.count) LazyList.empty
792+
else {
793+
init.evaluate(index)
794+
LazyList.fill(1)(index) #::: gen(index + 1)
795+
}
796+
}
797+
798+
LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation
799+
}
800+
assertRepeatedlyLazy(tailInitFactory)
775801
}
776802

777803
@Test

test/junit/scala/collection/immutable/LazyListTest.scala

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

0 commit comments

Comments
 (0)