Skip to content

Instantly share code, notes, and snippets.

@slouc
Last active June 11, 2020 16:41
Show Gist options
  • Save slouc/e76228fae43040ccc9240279831cf042 to your computer and use it in GitHub Desktop.
Save slouc/e76228fae43040ccc9240279831cf042 to your computer and use it in GitHub Desktop.
* `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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment