diff --git a/Data/HashMap/Base.hs b/Data/HashMap/Base.hs index 89d20463..7068d3f5 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 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 +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