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

我的梦境 提交于 2021-01-02 06:11:07

问题


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 ?


回答1:


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)




回答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)



来源:https://stackoverflow.com/questions/60128207/whats-the-difference-between-lazylist-and-stream-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!