Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded unionWith for different value types #488

Open
Benjamin-McRae-Tracsis opened this issue Feb 16, 2024 · 4 comments
Open

Expanded unionWith for different value types #488

Benjamin-McRae-Tracsis opened this issue Feb 16, 2024 · 4 comments

Comments

@Benjamin-McRae-Tracsis
Copy link

Hi, I think there's space for a unionWith with a signature like unionWith' :: 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.

@phadej
Copy link
Contributor

phadej commented May 12, 2024

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.

@Benjamin-McRae-Tracsis
Copy link
Author

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.

@phadej
Copy link
Contributor

phadej commented May 16, 2024

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.

@Benjamin-McRae-Tracsis
Copy link
Author

merge-style function would be nice to add to unordered-containers so we could do things like this, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants