We use monoids all the time without realizing it—string concatenation, sums of integers, products, Boolean operations, lists, and so on—they are all examples of monoids. Let's look at integer addition:
- Our type: Int.
- Our associative operation: add. It is indeed associative because ((1 + 2) + 3) == (1 + (2 + 3)).
- Our identity element: 0. It does nothing when added to another integer.
We can easily come up with similar examples for string concatenation, where the identity element will be an empty string, or for list concatenation, where the identity element will be an empty list, and many others. Similar examples can be found absolutely everywhere.
Everything we mentioned previously takes us to the following Scala representation ...