序列与 LazyList [英] Sequence vs LazyList

查看:13
本文介绍了序列与 LazyList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解序列和 LazyList 之间的差异.他们既懒惰又可能无限.虽然 seq<'T> 是来自 .NET 框架的 IEnumerable<'T>LazyList 包含在 F# PowerPack.在实践中,我遇到的序列比 LazyLists 更频繁.

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 LazyLists.

它们在性能、使用、可读性等方面有何不同?与 seq 相比,LazyList 名声如此糟糕的原因是什么?

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.

这些文档可能值得一看阅读.

These docs may be worth a read.

这篇关于序列与 LazyList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆