Skip to content

Instantly share code, notes, and snippets.

* `Foldable` folds things that form a `Monoid`; `fold` needs an identity and a binary op, and `Monoid` has both.
* `Reducible` reduces things that form a `Semigroup`; `reduce` only needs the binary op.
* Collections that can be empty (e.g. `List`) are `Foldable`. `fold` deals with emptiness by requiring an identity element.
* Collections that cannot be empty (e.g. `NonEmptyList`) are not only `Foldable`, but also `Reducible`. `reduce` is not good at dealing with emptiness (standard Scala's `reduce` blows up), but with non-empty collections that's not a problem.
Note: Folding a non-empty collection of elements that form a `Monoid` will use the `head` of collection as a starting element, and not `Monoid`'s identity.