Summary
In this chapter, we have explored two basic algebraic structures that occur frequently in functional programs: semigroups and monoids. While these are interesting patterns on their own, they turn out to be particularly useful for aggregating the elements of collections. For that purpose, we have introduced the notion of type constructors, types of higher kinds that allow us to separate the collection type from its element type. Next, we have explored the Foldable
constructor type class for collections whose elements can be extracted into a list. Its minimal complete definition based on foldMap
turns out to be a Swiss Army knife that can be conveniently instantiated with different monoids to accomplish all manner of aggregation tasks. Finally, we have developed a custom monoid to tackle a new task, checking the sortedness of foldable collections.
Chapter 10, Functors, Applicative Functors, and Traversables, continues with a hierarchy of type classes for type constructors...