# FSharpPlus Foldable A foldable in FSharpPlus is data structure that can be folded into a summary type. Think multiple values which are combined into a [[Monoid]]. The basic component of a foldable is a `FoldMap` member which has the following signature: ```fsharp static member inline FoldMap( l : 'foldable<'a>, f : 'a -> 'monoid<'a>) : 'monoid<'a> ``` For every containing value, `FoldMap` is expected to construct an arbitrary monoid using `f`, and then combine those monoids. Note that the implementation of `FoldMap` comes down to combining monoids, which can inherently be done in two different ways as the monoid's magma is not necessarily commutative. Either we can call `value ++ summary`, or `summary ++ value`. `FoldMap`-s which call `value ++ summary` are known as a *back folds*, while `summary ++ value` folds are known as just *folds*. It seems typical for `FoldMap`-s to be implemented in a back fold manner. ## `FoldBack` FSharpPlus `foldBack` operator requires a static member with the following signature: ```fsharp static member inline FoldBack( F : 'foldable<'a>, folder : 'a -> 'state -> 'state, zero : 'state) : 'state ``` Notice the parameter order of the `folder`. If `state` were a monoid (it usually is), and if we lifted the element into the monoid, and if the folder is monoid composition, then the composition would be called like `value ++ state`. The order is different with the regular [[#`Fold`|`Fold`]]. Also notice that `'state` does not have to be a monoid, although an [[FSharpPlus Endo|Endo]] monoid can be constructed by using `zero` and partially applying the `folder`. That is exactly what FSharpPlus `FoldBack.FromFoldMap` function is doing: 1. First it takes the folder and composes the [[FSharpPlus Endo|Endo]] constructor on it. The result is `'a -> Endo<'state>`. 2. Because Endo is a monoid, the composed function can now be passed to `FoldMap`. 3. The result of the above is `Endo<'state>`, which is called with the `zero` state ## `Fold` FSharpPlus `fold` operator requires a static member with the following signature: ```fsharp static member inline Fold( F : 'foldable<'a>, folder : 'state -> 'a -> 'state, zero : 'state) : 'state ``` Notice the parameter order of the `folder`. If `state` were a monoid (it usually is), and if we lifted the element into the monoid, and if the folder is monoid composition, then the composition would be called like `state ++ value`. The order is different with [[#`FoldBack`|`FoldBack`]]. Similar to `FoldBack`, `Fold` can also be constructed from `FoldMap`, although the implementation is a bit more complicated (this assumes `FoldMap` is implemented in a back fold fashion): 1. `Fold.FromFoldMap` first flips the folder, so that it has a function which can be composed with the Endo constructor. The result is `'a -> 'state -> 'state`. 2. The above function is composed to the Endo constructor, the result being `'a -> Endo<'state>`. 3. If were to call `FoldMap` now, `Fold` wouldn't be any different from `FoldBack`. For it to be different we need to ensure the order of elements passed to monoid's magma is flipped. That is exactly what [[FSharpPlus Dual|Dual]] does, so we'll compose the Dual constructor to the above. The result is then `'a -> Dual<Endo<'state>>`, which is also a monoid which we can pass to `FoldMap`. 4. We unpack the Endo inside the resulting Dual, and run it with `zero` state and we're done.