hey*_*you 7 scala lazy-evaluation scala-collections
我是 Scala 的新手,我刚刚了解到它LazyList是为了替换而创建的Stream,同时他们将.view方法添加到所有集合中。
所以,我想知道为什么被LazyList添加到 Scala 集合库中,我们什么时候可以做List.view?
我刚刚看了 Scaladoc,似乎唯一的区别是LazyList有记忆,而View没有。我是对还是错?
Stream除了第一个(头)元素外,其他元素都是惰性实现的。这被视为一种缺陷。
一个List视图被懒惰地重新评估,但据我所知,必须首先完全实现。
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>)
Run Code Online (Sandbox Code Playgroud)
在 2.13 中,您不能强制从视图返回原始集合类型:
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))
Run Code Online (Sandbox Code Playgroud)
_.toList如果调用者需要选择结果类型,则采用视图并可选地返回严格实现的函数还必须采用“强制函数”,例如。
我在日常工作中不会做这种事情,但这种行为让我感到惊讶。
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |