What is the difference between List.view and LazyList?

安稳与你 提交于 2020-05-14 19:26:56

问题


I am new to Scala and I just learned that LazyList was created to replace Stream, and at the same time they added the .view methods to all collections.

So, I am wondering why was LazyList added to Scala collections library, when we can do List.view?

I just looked at the Scaladoc, and it seems that the only difference is that LazyList has memoization, while View does not. Am I right or wrong?


回答1:


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

A List view is re-evaluated lazily but, as far as I know, has to be completely realized first.

def bang :Int = {print("BANG! ");1}

LazyList.fill(4)(bang)  //res0: LazyList[Int] = LazyList(<not computed>)
Stream.fill(3)(bang)    //BANG! res1: Stream[Int] = Stream(1, <not computed>)
List.fill(2)(bang).view //BANG! BANG! res2: SeqView[Int] = SeqView(<not computed>)



回答2:


In 2.13, you can't force your way back from a view to the original collection type:

scala> case class C(n: Int) { def bump = new C(n+1).tap(i => println(s"bump to $i")) }
defined class C

scala> List(C(42)).map(_.bump)
bump to C(43)
res0: List[C] = List(C(43))

scala> List(C(42)).view.map(_.bump)
res1: scala.collection.SeqView[C] = SeqView(<not computed>)

scala> .force
            ^
       warning: method force in trait View is deprecated (since 2.13.0): Views no longer know about their underlying collection type; .force always returns an IndexedSeq
bump to C(43)
res2: scala.collection.IndexedSeq[C] = Vector(C(43))

scala> LazyList(C(42)).map(_.bump)
res3: scala.collection.immutable.LazyList[C] = LazyList(<not computed>)

scala> .force
bump to C(43)
res4: res3.type = LazyList(C(43))

A function taking a view and optionally returning a strict realization would have to also take a "forcing function" such as _.toList, if the caller needs to choose the result type.

I don't do this sort of thing at my day job, but this behavior surprises me.



来源:https://stackoverflow.com/questions/59587449/what-is-the-difference-between-list-view-and-lazylist

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