winitzki / sofp

A free book: "The Science of Functional Programming"
GNU General Public License v2.0
1.41k stars 97 forks source link

Stream deprecation #22

Closed philipschwarz closed 4 years ago

philipschwarz commented 4 years ago

Hell Sergei,

I am continuing to find your book very nice/useful and I again ended up referencing sections of it heavily: https://www.slideshare.net/pjschwarz/the-functional-programming-triad-of-fold-scan-and-iterate https://www.slideshare.net/pjschwarz/folding-unfolded-polyglot-fp-for-fun-and-profit-haskell-and-scala-part-4

This PR is just to point out (if pointing out is needed - you may well know this already), that Stream is now deprecated in favour of LazyList: "Deprecated (Since version 2.13.0) Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)" https://www.scala-lang.org/api/current/scala/collection/immutable/Stream.html

LazyList also has an iterate function: https://www.scala-lang.org/api/current/scala/collection/immutable/LazyList$.html#iterate[A](start:=%3EA)(f:A=%3EA):scala.collection.immutable.LazyList[A]

(https://www.scala-lang.org/api/current/scala/collection/immutable/Stream.html says "Deprecated (Since version 2.13.0) Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)"

I don't know if you want to update the book to use LazyList instead of Stream (btw, I think I find the name LazyList a bit odd - a bit distracting - I like the plainness of Stream). It looks like this old post says Stream will be removed in 2.14: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html

Philip

winitzki commented 4 years ago

Hi Philip, @philipschwarz Thank you for bringing this to my attention. Yes, I know we are supposed to use LazyList instead of the deprecated Stream. However, there are some use cases for non-lazy head and lazy tail, as in Stream. I am sure there will be applications that depend on the not-fully-lazy nature of Stream; just last week I found one such use case in my production code. I expect people to implement their own Stream under a different name. So, I am reluctant to remove Stream from the book.

Also, I think the book largely does not depend on details of lazy vs. eager vs. on-call evaluation. These details may be important in certain corner cases. Currently, the book assumes eager evaluation of everything, - that is, everything is a val. Lazy values (lazy val x = ...) or on-call values (def x = ...) are to be indicated explicitly when they occur.

I think I will just add a footnote about the deprecation of Stream. For all we know, LazyList may become deprecated in Scala 3.1 in favor of some other thing du jour...

philipschwarz commented 4 years ago

@winitzki yes, that all makes perfect sense 👌