C# Enumerable.Reverse().Aggregate(initval, func) 是真正的右折叠吗?

发布于 2024-10-06 20:58:42 字数 205 浏览 0 评论 0原文

维基百科说 Reverse().Aggregate(initval, func) 是右折叠。这听起来好像不是真的,而是一种廉价的逃避……任何人都可以评论这个问题吗? C# 有正确的折叠吗?

Wikipedia says that Reverse().Aggregate(initval, func) is a Right Fold. This sounds like it is not true, but rather a cheap cop out... Can anyone comment on this issue? Does C# have right folds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-10-13 20:58:42

获取右折叠的定义并检查 Reverse().Aggregate(initval, func) 是否适合它。

定义:

将第一个元素与其余元素的组合结果称为右折叠

因此,如果您想计算 (1, 2, 3) 的总和。如果您只是聚合,则评估将为(1 + 2) + 3。如果您Reverse().Aggregate,那么它将是(3 + 2) + 1,这完全符合定义。

问题可能是它是否有效,因为 Reverse 是昂贵的操作,但从功能上来说,它是完美的右折叠。

Take definition of right fold and check whether Reverse().Aggregate(initval, func) fits into it.

Definition:

Combining the first element with the results of combining the rest is called a right fold

So if you want to calc the sum of (1, 2, 3). If you just Aggregate the evaluation will be (1 + 2) + 3. If you Reverse().Aggregate then it will be (3 + 2) + 1, which perfectly fits the definition.

The question might be is it efficient because Reverse is expensive operation, but functionally it is perfect right fold.

败给现实 2024-10-13 20:58:42

Aggregate 是真正的左折叠。聚合反向列表与任何可逆列表上的完整(非惰性)右折叠具有相同的语义。

Aggregate is a true left fold. Aggregating a reversed list has the same semantics as a complete (non-lazy) right fold on any reversible list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文