Semigroup

A Semigroup is an object that express an binary associative operation. It is defined for a set, for example, like the set of Int in scala. This type of design pattern may seem so high-level as to be useless. However, it expresses the most basic idea of append. for the set it describes. If you add numbers together, add items to a list or "add" together two of your domain objects then a Semigroup expresses this concept.

scala> val s = Semigroup[Int]
val s = Semigroup[Int]
s: scalaz.Semigroup[Int] = scalaz.std.AnyValInstances$$anon$5@52e4f6fc

scala> s.append(1,3)
s.append(1,3)
res17: Int = 4

scala> val slist = Semigroup[List[Int]]
val slist = Semigroup[List[Int]]
slist: scalaz.Semigroup[List[Int]] = scalaz.std.ListInstances$$anon$4@691edde

scala> slist.append(List(1,2), List(3,4))
slist.append(List(1,2), List(3,4))
res19: List[Int] = List(1, 2, 3, 4)

Last updated