Open Benjamin-McRae-Tracsis opened 4 months ago
That's what alignWith
from semialign
package does. The (a -> c) -> (b -> c) -> (a -> b -> c)
is a way to represent These a b -> c
.
(EDIT: HM.unionWith' This That These
is align
).
So if you are already using these
, I don't see a reason for you to not depend on semialign
.
The case I was using was just one example which could use semialign
's alignWith
, but I feel like it would be a useful function to have without relying on other packages.
FWIW, https://hackage.haskell.org/package/containers-0.7/docs/Data-Map-Merge-Lazy.html#v:merge has in its docs
unionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
The alignWith
in semialign
is defined as
alignWith f = Map.merge (Map.mapMissing (\_ x -> f (This x)))
(Map.mapMissing (\_ y -> f (That y)))
(Map.zipWithMatched (\_ x y -> f (These x y)))
so the building blocks are there.
merge
-style function would be nice to add to unordered-containers
so we could do things like this, yes.
Hi, I think there's space for a
unionWith
with a signature likeunionWith' :: Hashable k => (a -> c) -> (b -> c) -> (a -> b -> c) -> HM.HashMap k a -> HM.HashMap k b -> HM.HashMap k c
.This differs from the existing unionWith by allowing different ways to transforming the input maps, and being more specific in how to combine maps with differing value types.
My current usage is generally
HM.unionWith' This That These
. This would be a more powerful function.