[Haskell-cafe] MonoidHashMap?

Sean Leather sean.leather at gmail.com
Tue Jan 20 14:15:12 UTC 2015


On Tue, Jan 20, 2015 at 2:33 PM, Konstantine Rybnikov wrote:

> I'm working with a lot of HashMap's and it's very frustrating how many
> times I've "lost" my data because of usage of either a Monoid instance of a
> HashMap (which is defined as H.union, which, upon collision, takes value
> from first hm and discards from second), or just using fromList in the
> wrong place. Whereas the data I'm working is is mostly defined as (Monoid v
> => HashMap k v), so what I need "by default" is actually something like
> `H.unionWith (<>)`.
>
> What I was wondering is this: is something like MonoidHashMap is desired
> to be in unordered-containers?


Definitely!

I have often wished the Monoid instances for both HashMap and Map had
`mappend = unionWith mappend` [1]. It may be trivial to write a newtype
wrapper and Monoid instance, but it's tedious to use many library functions
with that wrapper.

This would be more useful to have in containers and unordered-containers,
rather than in separate packages, for discoverability and maintainability.

Regards,
Sean

[1] Assuming that was already the case, I believe we could get the `mappend
= union` approach by coercing the value type to Data.Monoid.First. Of
course, there are other useful monoids for the taking, as well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150120/cd67b28d/attachment.html>


More information about the Haskell-Cafe mailing list