Monoid

Monoid is another popular type class that you will frequently encounter in practice. Basically, it defines how to combine two data types.

As an example of Monoid, let's take a look at the implementation of an Applicative type class for the Either data type. In the previous section, we were forced to use a specific version of Either, the one with Left set to a list of strings. This was done precisely because we know how to combine two lists of strings, but we do not know how to combine any two generic types.

If we define the signature of this preceding Applicative as follows, then we will not be able to provide a decent implementation of this function because we will not be able to combine two generic types:

implicit def applicative[L]: ...

Get Mastering Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.