News for hackers. Stuff that matters.
Re:Frege: A Haskell-Like Language for the JVM by whateveracct on Thursday August 13, 2015 @07:38PM (#10057838)
Monoids are much easier. You just need a binary associative operator |+| (+,*, min, max all work for ints, ++ for lists/vectors, for instance) and some notion of an identity for the type s.t. forall a: A, a |+| identity === identity |+| a === a

They are HUGELY useful. One of the most useful is a "union" monoid definition for Maps. In Scala, it's written like this:

implicit def mapUnionMonoid[K, V: Semigroup]: Monoid[Map[K,V]]

So any map with Semigroup values is a monoid (Semigroups are monoids without the zero value). |+| under this definition will combine the Maps, but in hte case of collisions, |+| the colliding values together. This Monoid is the ABSOLUTE KING of aggregation. You can foldMap over lists and produce singleton Maps of the shape you want, and let the monoid instance aggregate for you.

There are no comments on this comment yet.