Monoids in Scalaz

One of the concepts we looked at in Chapter 10, Functional Design Patterns - the Deep Theory, was monoids. We defined a trait and some rules for them and then showed examples of how to use them and what they are good for. In these examples, we defined monoids for integer addition and multiplication as well as string concatenation. Scalaz already has a Monoid trait that we can use to write our own monoids. Also, this trait has a few monoids that we have defined before already implemented:

import scalaz.Monoidpackage object monoids {    // Int addition and int multiplication exist already,  // so we will show them in an example.  val stringConcatenation = new Monoid[String] {    override def zero: String = ""    override def append ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.