List.view和LazyList有什么区别?

List.view和LazyList有什么区别?

问题描述:

我是Scala的新手,我刚刚了解到创建了 LazyList 来替换 Stream ,并且他们同时添加了 .view 所有集合的方法.

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.

所以,我想知道为什么在我们可以执行 List.view 时,将 LazyList 添加到Scala集合库中?

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

我只是看了Scaladoc,看来唯一的区别是 LazyList 具有备注,而 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?

Stream 元素被懒惰地实现,除了第一(头部)元素.这被视为一种缺陷.

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

List 视图被懒惰地重新评估,但据我所知,必须首先完全实现.

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