序列与LazyList [英] Sequence vs LazyList
问题描述
我无法围绕序列与 LazyList
之间的区别。他们都是懒惰的,潜在的无限的。而 seq<'T>
是.NET框架中的 IEnumerable<'T>
, LazyList
包含在 F#PowerPack 中。实际上,我遇到的次序比 LazyList
s更多。
I can't wrap my head around the differences between sequence and LazyList
. They're both lazy and potentially infinite. While seq<'T>
is IEnumerable<'T>
from .NET framework, LazyList
is included in F# PowerPack. In practice, I encounter sequences much more often than LazyList
s.
他们的区别是什么的性能,使用,可读性等?与 seq
相比, c
What are their differences in terms of performance, usage, readability, etc? What are reasons for such a bad reputation of LazyList
compared to that of seq
?
推荐答案
LazyList
仅计算每个元素一次,而不管遍历列表多少次。以这种方式,它更接近于从 Seq.cache
(而不是典型的序列)返回的序列。但是,除了缓存之外, LazyList 的行为完全像一个列表:它使用一个列表结构,并支持模式匹配。所以你可能会说:当你需要列表语义和缓存(除了懒惰)之后,使用 LazyList
而不是 seq
。
LazyList
computes each element only once regardless of how many times the list is traversed. In this way, it's closer to a sequence returned from Seq.cache
(rather than a typical sequence). But, other than caching, LazyList
behaves exactly like a list: it uses a list structure under the hood and supports pattern matching. So you might say: use LazyList
instead of seq
when you need list semantics and caching (in addition to laziness).
关于无限, seq
的内存使用情况是不变的,而 LazyList
是线性的。
Regarding both being infinite, seq
's memory usage is constant while LazyList
's is linear.
这些 docs 可能值得一读。
这篇关于序列与LazyList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!