What's the difference between LazyList and Stream in Scala?
Asked Answered
C

2

13

I noticed that Stream is deprecated in Scala 2.13 and they suggest using LazyList. They also say "Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)".
What does it exactly mean ? Why did they deprecate Stream ?

Cardenas answered 8/2, 2020 at 15:24 Comment(4)
Basically Stream was only lazy on its tail, so the head was always computed. For many people this was surprising and lead to erroneous code. On the other hand LayzList is fully lazy. It doesn't compute the head until it is called. Note that when you compute the head of a LayzList also computes its tail and viceversa.Twibill
Got it. Thank you.Cardenas
@LuisMiguelMejíaSuárez Is there an example of erroneous code it would lead to?Overlarge
@MarioGalic not erroneous in the sense of correctness. But erroneous in the sense of how it was behaving.Twibill
O
12

NthPortal, a contributor to LazyList, states in Update and improve LazyList docs #7842

The key difference between LazyList and Stream - and its key feature - is that whether or not it is lazy is evaluated lazily. I'm not sure how best to convey that.

jwvh states in related question

Stream elements are realized lazily except for the 1st (head) element. That was seen as a deficiency.

Scala 2.13 release notes state

immutable.LazyList replaces immutable.Stream. Stream had different laziness behavior and is now deprecated. (#7558, #7000)

Overlarge answered 8/2, 2020 at 17:19 Comment(0)
H
2

As per the blog post:

LazyList Is Preferred Over Stream

Stream is deprecated in favor of LazyList. As its name suggests, a LazyList is a linked list whose elements are lazily evaluated. An important semantic difference with Stream is that in LazyList both the head and the tail are lazy, whereas in Stream only the tail is lazy.

Also in the Stream documentation:

Deprecated (Since version 2.13.0)

Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)

Homebrew answered 23/6, 2020 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.