DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Trending

  • AI-Assisted Coding for iOS Development: How Tools like CursorAI Are Redefining the Developer Workflow
  • Securing Parquet Files: Vulnerabilities, Mitigations, and Validation
  • How Clojure Shapes Teams and Products
  • A Complete Guide to Modern AI Developer Tools
  1. DZone
  2. Coding
  3. Languages
  4. Diving into Scala Cats – Monoids

Diving into Scala Cats – Monoids

In this article, we'll dive into Cats to understand Monoid type class.

By 
Mansi Babbar user avatar
Mansi Babbar
·
Jan. 31, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
2.9K Views

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:

Scala
 




x


 
1
def combineStrings(collection: Seq[String]): String = {
2
  collection.foldLeft("")(Semigroup[String].combine)
3
}


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 A (”” for String, 0 for Int, etc).

Monoid comes up with a solution to this shortcoming, by introducing an identity/empty element.

The signature of Monoid can be specified as:

Scala
 




xxxxxxxxxx
1


 
1
trait Monoid[A] extends Semigroup[A] {   
2
  def empty: A 
3
}


How does identity element solves the limitation of Semigroups?

The empty 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:

Scala
 




xxxxxxxxxx
1


 
1
def combineAll[A](collection: Seq[A])(implicit ev: Monoid[A]): A = {
2
  val monoid = Monoid[A]
3
  collection.foldLeft(monoid.empty)(monoid.combine)
4
}


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 combine operation:

Scala
 




xxxxxxxxxx
1


 
1
combine(x, empty) = combine(empty, x) = x


For example:

Scala
 




xxxxxxxxxx
1


 
1
// Integer addition using 0 as an identity (Rules valid)
2
1 + 0 == 0 + 1 == 1
3

          
4
// Integer multiplication using 1 as an identity (Rules valid)
5
2 * 1 == 1 * 2 == 2
6

          
7
// Integer multiplication using 0 as an identity (Rules invalid)
8
2 * 0 == 0 * 2 != 2 


So it’s clear the empty/identity element depends on the context not just on the type. That’s why Monoid (and Semigroup) implementations are specific not only to the type but also the combine operation.

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.
Stay tuned!!!

Learning Template

You can download the learning template Scala Cats - Monoids demonstrating the code implementations provided in this article.

References

The best two Scala Cats resources I know are here:

  • The Cats library is available at github.com/typelevel/cats
  • The book, Advanced Scala with Cats, is available at underscore.io/books/advanced-scala/
Scala (programming language)

Published at DZone with permission of Mansi Babbar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!