Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top