September 2018
Intermediate to advanced
398 pages
9h 43m
English
In the type class introduction, we defined a Combine type class. It turns out that this type class is already defined in the Cats library. Its name is Semigroup; this name comes from the mathematical representation of this algebraic structure.
Open it in IntelliJ to see how it is defined:
/** * A semigroup is any set `A` with an associative operation (`combine`). */ trait Semigroup[@sp(Int, Long, Float, Double) A] extends Any with Serializable { /** * Associative operation taking which combines two values. */ def combine(x: A, y: A): A (...) }
The @sp annotation is an optimization to avoid the boxing/unboxing of primitive types. Apart from that, the definition of Semigroup is the same as our Combine type class.
Read now
Unlock full access