Monoids

All monoids, monads, and functors are derived from mathematics. One thing about this subject is that similarly to programming, it tries to find abstractions. If we try to map mathematics to programming, we can think about the different data types we have—Int, Double, Long, or custom data types. Each type can be characterized by the operations it supports and the laws of these operations, and this is called the algebra of the type.

Now, if we think about it, we can identify the operations that are shared by multiple types, for example, addition, multiplication, subtraction, and so on. Different types can share the same operations and they can conform to exactly the same laws. This is something we can take advantage of because this allows ...

Get Scala Design Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.