September 2018
Intermediate to advanced
398 pages
9h 43m
English
A Monoid is Semigroup with an additional empty function, also called an identity element.
The following is an extract of this trait's definition in Cats:
trait Monoid[@sp(Int, Long, Float, Double) A] extends Any with Semigroup[A] { /** * Return the identity element for this monoid. */ def empty: A
The Monoid trait extends the Semigroup trait. As such, it has all of the methods of Semigroup, plus this additional empty method. We have already seen several examples of the combine operation of Semigroup's for different types.
Let’s see what happens when we call empty for the same types, as follows:
import cats.implicits._ import cats.kernel.Monoid Monoid[Int].empty // res0: Int = 0 Monoid[String].empty // res1: String = Monoid[Option[Double]].empty ...
Read now
Unlock full access