Diving into Scala Cats – Monoids
In this article, we'll dive into Cats to understand Monoid type class.
Join the DZone community and get the full member experience.Join For Free
Today we are going to deep dive into the Scala Cats again to find out about Monoids. If you haven’t already read my post about Diving into Scala Cats – Semigroups I suggest you do so now.
What are Monoids?
The Monoid extends Semigroup and adds a default or fallback value for the given type.
Monoid type class comes with two methods – one is the
combine method of Semigroups, and another one is the
empty method that performs identity operation.
In the post about Semigroups, we saw an example where we’re using Semigroups along with Scala’s
fold() to operate on a collection of values:
We discussed the limitation of Semigroups where we cannot write a generic method
combineAll(collection: Seq[A]): [A] for the above expression because the fallback value will depend on the type of
Monoid comes up with a solution to this shortcoming, by introducing an identity/empty element.
The signature of
Monoid can be specified as:
How does identity element solves the limitation of Semigroups?
String that we are passing in
combineStrings is known as the identity or empty value, we can think of it as a fallback or default value. It resolves the shortcoming of Semigroups.
Now we can easily provide the implementation of generic method combineAll(collection: Seq[A]): [A] using Monoids:
Monoids hold Associativity and Identity Laws
Since Semigroups follow principle of associativity, same rules with some add-ons are applied to Monoids as well.
combine operation has to be associative and
empty value should be an identity for the
So it’s clear the
empty/identity element depends on the context not just on the type. That’s why
Semigroup) implementations are specific not only to the type but also the
Cats allows combining Monoids together to form bigger Monoids and write more generalized functions which will take something composable instead of some concrete types.
We’ll learn about more core concepts of Cats in our upcoming article.
You can download the learning template Scala Cats - Monoids demonstrating the code implementations provided in this article.
The best two Scala Cats resources I know are here:
Published at DZone with permission of Mansi Babbar. See the original article here.
Opinions expressed by DZone contributors are their own.