Proposal: more general unionWith for Data.Map
Milan Straka
fox at ucw.cz
Tue Jan 24 20:55:13 CET 2012
> > Is union really an appropriate name here? I expect the following to hold:
> >
> > forall k ∊ (keys m1) ∪ (keys m2) => k ∊ (m1 ∪ m2)
> >
> > This means that keys must not be deleted by the union operator.
> > Perhaps 'merge' is a better name.
> >
> > Cheers,
> > Johan
>
> I don't care much about the naming, but note that the analogous
> property already fails for the generalized intersectionWithKey in the
> development version.
It is true that
forall k ∊ (keys m1) ∩ (keys m2) => k ∊ (m1 ∩ m2)
fails, but this expression is not really correct for
intersectionWithKey, as it intersects Map k a and Map k b,
so Map k a ∩ Map k b is not well defined.
Anyway, we could instead of proposed unionWithKey offer functions
mergeWith :: Ord k => (Maybe a -> Maybe b -> Maybe c) -> Map k a -> Map k b -> Map k c
mergeWithKey :: Ord k => (k -> Maybe a -> Maybe b -> Maybe c) -> Map k a -> Map k b -> Map k c
The combining function is executed for every unique key from both maps.
All functions unionWith, intersectionWith, differencseWith, proposed
unionWith can be expressed using this function.
But I am not sure about the performance (using so many Maybes).
Cheers,
Milan
More information about the Libraries
mailing list