开发者

Is C# Enumerable.Reverse().Aggregate(initval, func) a true Right Fold?

开发者 https://www.devze.com 2023-01-29 19:00 出处:网络
开发者_JAVA百科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 rig

开发者_JAVA百科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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消