Proposal: more general unionWith for Data.Map

Milan Straka fox at
Tue Jan 24 22:25:57 CET 2012

> On Tue, Jan 24, 2012 at 11:58 AM, Milan Straka <fox at> wrote:
> > FYI, the new intersectionWithKey is not released yet -- maybe we could
> > leave intersectionWithKey as it is, and provide mergeWith[Key] instead.
> > Opinions?
> I think so. We should avoid the words union and intersection if they
> don't correspond to the mathematical idea, or we risk confusing
> people.

I think that with intersectionWithKey, we are still holding to the
mathematical idea. Imagine I have a Map Hash List. When performing
intersection of two such maps, it can happen that for one hash there are
different values in the maps and so in the intersection the has is not

Generally if the Map k a is representing a set using both k and a,
the intersection on the same key can still be empty.

But this is not so in union -- if key is any of maps, it should be in
the result.

So personally I would generalize the combining function of
intersectionWith to (a -> b -> Maybe c),
but not generalize the combining function of unionWith, and instead
provide mergeWith with combining function (a -> a -> Maybe a).
(Similarly for *Key variants).


More information about the Libraries mailing list