MonoidK
API Documentation: MonoidK
MonoidK
is a universal monoid which operates on type constructors of one argument.
This type class is useful when its type parameter F[_]
has a
structure that can be combined for any particular type, and which
also has an "empty" representation. Thus, MonoidK
is like a Monoid
for type constructors (i.e. parametrized types).
A MonoidK[F]
can produce a Monoid[F[A]]
for any type A
.
Here's how to distinguish Monoid
and MonoidK
:
Monoid[A]
allowsA
values to be combined, and also means there is an "empty" A value that functions as an identity.
MonoidK[F]
allows twoF[A]
values to be combined, for anyA
. It also means that for anyA
, there is an "empty"F[A]
value. The combination operation and empty value just depend on the structure ofF
, but not on the structure ofA
.
Let's compare the usage of Monoid[A]
and MonoidK[F]
.
First some imports:
import cats.{Monoid, MonoidK}
import cats.syntax.all._
Just like Monoid[A]
, MonoidK[F]
has an empty
method, but it is parametrized on the type of the element contained in F
:
Monoid[List[String]].empty
// res0: List[String] = List()
MonoidK[List].empty[String]
// res1: List[String] = List()
MonoidK[List].empty[Int]
// res2: List[Int] = List()
And instead of combine
, it has combineK
, which also takes one type parameter:
Monoid[List[String]].combine(List("hello", "world"), List("goodbye", "moon"))
// res3: List[String] = List("hello", "world", "goodbye", "moon")
MonoidK[List].combineK[String](List("hello", "world"), List("goodbye", "moon"))
// res4: List[String] = List("hello", "world", "goodbye", "moon")
MonoidK[List].combineK[Int](List(1, 2), List(3, 4))
// res5: List[Int] = List(1, 2, 3, 4)
Actually the type parameter can usually be inferred:
MonoidK[List].combineK(List("hello", "world"), List("goodbye", "moon"))
// res6: List[String] = List("hello", "world", "goodbye", "moon")
MonoidK[List].combineK(List(1, 2), List(3, 4))
// res7: List[Int] = List(1, 2, 3, 4)
MonoidK
extends SemigroupK
, so take a look at the SemigroupK
documentation for more examples.