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

Conversation

NthPortal
Copy link
Contributor

@NthPortal NthPortal commented May 14, 2020

Change #:: for LazyList so that the tail computation
is wrapped as a state computation for a new LazyList,
thus deferring its execution as long as possible.

Fixes scala/bug#11984


Before (2.13.2-):

scala 2.13.2> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.2> res1.head
hey
val res5: Int = 1

After (2.13.3+):

scala 2.13.3> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res1.head
val res5: Int = 1

@NthPortal NthPortal added library:collections PRs involving changes to the standard collection library bug fix labels May 14, 2020
@scala-jenkins scala-jenkins added this to the 2.13.4 milestone May 14, 2020
@NthPortal NthPortal requested review from SethTisue and sjrd May 14, 2020 05:35
@NthPortal NthPortal modified the milestones: 2.13.4, 2.13.3 May 14, 2020
@NthPortal
Copy link
Contributor Author

Interestingly, #::: is already sufficiently lazy and doesn't need to be changed. I found it quite difficult to reason about this, however, and was only fully convinced of it after writing a test to prove it.

Change `#::` for `LazyList` so that the tail computation
is wrapped as a state computation for a new `LazyList`,
thus deferring its execution as long as possible.
@SethTisue SethTisue added the release-notes worth highlighting in next release notes label May 14, 2020
@NthPortal NthPortal requested a review from julienrf May 16, 2020 21:50
@SethTisue SethTisue merged commit a8a7261 into scala:2.13.x May 20, 2020
@NthPortal NthPortal deleted the topic/ll-cons-laziness/PR branch May 20, 2020 05:00
@SethTisue
Copy link
Member

@NthPortal should LazyList.cons have changed as well? see discussion at https://www.reddit.com/r/scala/comments/hgv8k3/lazylist_or_lazylistcons/

scala 2.13.3> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res1.head
val res5: Int = 1

scala 2.13.3> LazyList.cons(1, { println("hey"); LazyList() })
val res6: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res6.head
hey
val res7: Int = 1

@NthPortal
Copy link
Contributor Author

yeah, probably :(

@NthPortal
Copy link
Contributor Author

see #9095

ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Aug 24, 2020
Port of scala/scala#8985.

Co-authored-by: NthPortal <nthportal@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library:collections PRs involving changes to the standard collection library release-notes worth highlighting in next release notes
Projects
None yet
5 participants