From 5a00fa518fc92815246c15531a69b92686721b1a Mon Sep 17 00:00:00 2001 From: Petr Pudlak Date: Fri, 22 Jan 2016 21:18:14 +0100 Subject: [PATCH 1/2] Add unionsWith to combine a list of hash maps --- Data/HashMap/Base.hs | 8 ++++++++ Data/HashMap/Lazy.hs | 1 + Data/HashMap/Strict.hs | 1 + tests/HashMapProperties.hs | 5 +++++ 4 files changed, 15 insertions(+) diff --git a/Data/HashMap/Base.hs b/Data/HashMap/Base.hs index 89d20463..b432776a 100644 --- a/Data/HashMap/Base.hs +++ b/Data/HashMap/Base.hs @@ -36,6 +36,7 @@ module Data.HashMap.Base , union , unionWith , unions + , unionsWith -- * Transformations , map @@ -773,6 +774,13 @@ unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v unions = L.foldl' union empty {-# INLINE unions #-} +-- | Construct a set containing all elements from a list of sets. +-- If a key occurs in both maps, the provided function (first argument) will be +-- used to compute the result. +unionsWith :: (Eq k, Hashable k) => (v -> v -> v) -> [HashMap k v] -> HashMap k v +unionsWith f = L.foldl' (unionWith f) empty +{-# INLINE unionsWith #-} + ------------------------------------------------------------------------ -- * Transformations diff --git a/Data/HashMap/Lazy.hs b/Data/HashMap/Lazy.hs index b64d3f40..991e133c 100644 --- a/Data/HashMap/Lazy.hs +++ b/Data/HashMap/Lazy.hs @@ -55,6 +55,7 @@ module Data.HashMap.Lazy , union , unionWith , unions + , unionsWith -- * Transformations , HM.map diff --git a/Data/HashMap/Strict.hs b/Data/HashMap/Strict.hs index ef443817..76e7e48b 100644 --- a/Data/HashMap/Strict.hs +++ b/Data/HashMap/Strict.hs @@ -55,6 +55,7 @@ module Data.HashMap.Strict , union , unionWith , unions + , unionsWith -- * Transformations , map diff --git a/tests/HashMapProperties.hs b/tests/HashMapProperties.hs index ab7e22a2..be8dc316 100644 --- a/tests/HashMapProperties.hs +++ b/tests/HashMapProperties.hs @@ -142,6 +142,10 @@ pUnions :: [[(Key, Int)]] -> Bool pUnions xss = M.toAscList (M.unions (map M.fromList xss)) == toAscList (HM.unions (map HM.fromList xss)) +pUnionsWith :: [[(Key, Int)]] -> Bool +pUnionsWith xss = M.toAscList (M.unionsWith (-) (map M.fromList xss)) == + toAscList (HM.unionsWith (-) (map HM.fromList xss)) + ------------------------------------------------------------------------ -- ** Transformations @@ -265,6 +269,7 @@ tests = , testProperty "union" pUnion , testProperty "unionWith" pUnionWith , testProperty "unions" pUnions + , testProperty "unionsWith" pUnionsWith -- Transformations , testProperty "map" pMap -- Folds From 340b55dd1ce5b398be7cf9786f67a683d22404e5 Mon Sep 17 00:00:00 2001 From: Petr Date: Wed, 8 Feb 2023 14:51:51 +0100 Subject: [PATCH 2/2] Fix a comment for `unionWith` in Data/HashMap/Base.hs - it should be 'maps', not 'sets'. Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- Data/HashMap/Base.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data/HashMap/Base.hs b/Data/HashMap/Base.hs index b432776a..7068d3f5 100644 --- a/Data/HashMap/Base.hs +++ b/Data/HashMap/Base.hs @@ -774,7 +774,7 @@ unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v unions = L.foldl' union empty {-# INLINE unions #-} --- | Construct a set containing all elements from a list of sets. +-- | Construct a map containing all elements from a list of maps. -- If a key occurs in both maps, the provided function (first argument) will be -- used to compute the result. unionsWith :: (Eq k, Hashable k) => (v -> v -> v) -> [HashMap k v] -> HashMap k v