Left and Right Folds

A fold combines values using an operation and a seed. Left and right folds group those applications differently.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Use a nonassociative operation
  2. State when regrouping is allowed

Use a nonassociative operation

With subtraction, left fold starting at zero on 1, 2, 3 produces \(((0-1)-2)-3=-6\). Right fold produces \(1-(2-(3-0))=2\). Loop accumulators naturally represent the left sequence of intermediate states.

State when regrouping is allowed

An associative operation with an appropriate identity can agree across these groupings when operand order is preserved. Floating-point addition may differ due to binary rounding, even though real addition is associative.[1]

References

  1. Python Software Foundation, Functional Programming HOWTO ↩