Description

Fold applies a function to a sequence in order, maintaining state via an accumulator. The Fold command returns either a single value or a new sequence.

In its first form, Fold operates like Reduce, returning a value by applying a combining function to each element in a sequence, passing the current element and the previous reduction result to the function. However, Fold has the following differences from Reduce:

- it is guaranteed to proceed through the sequence from first element to last.
- it passes an initial base value to the function with the first element in
  place of the previous reduction result.

In its second form, Fold operates like ConcatMap, returning a new sequence rather than a single value. When an emit function is provided, Fold will:

- proceed through the sequence in order and take an initial base value, as above.
- for each element in the sequence, call both the combining function and a
  separate emitting function with the current element and previous reduction result.
- optionally pass the result of the combining function to the emitting function.

If provided, the emitting function must return a list.