[RFC] New MonoidMap type

Thanks for sharing this viewpoint! I also like the “weighted set” intuition.

By adjusting the monoidal value type, we can reproduce several similar types (and use newtype deriving to get instances of Semigroup, Monoid, and relevant subclasses):

newtype Map k v = Map (MonoidMap k (First  v))
newtype Set k   = Set (MonoidMap k (Maybe ()))

newtype       MultiSet k =       MultiSet (MonoidMap k (Sum Natural))
newtype SignedMultiSet k = SignedMultiSet (MonoidMap k (Sum Integer))

newtype     MultiMap k v =     MultiMap (MonoidMap k (Set v))
newtype ListMultiMap k v = ListMultiMap (MonoidMap k     [v])

Regarding boring use cases, I’m 100% sure you’ve ran into the task of having to aggregate a bunch of values in some form (e.g. into a list, sum them, only take the first one, etc) based on their keys. This package would be extremely useful then, to reduce some of the “boilerplate” that usually arises there.

1 Like