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

Add unionsWith to combine a list of hash maps #118

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Data/HashMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Data.HashMap.Base
, union
, unionWith
, unions
, unionsWith

-- * Transformations
, map
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Data.HashMap.Lazy
, union
, unionWith
, unions
, unionsWith

-- * Transformations
, HM.map
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Data.HashMap.Strict
, union
, unionWith
, unions
, unionsWith

-- * Transformations
, map
Expand Down
5 changes: 5 additions & 0 deletions tests/HashMapProperties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -265,6 +269,7 @@ tests =
, testProperty "union" pUnion
, testProperty "unionWith" pUnionWith
, testProperty "unions" pUnions
, testProperty "unionsWith" pUnionsWith
-- Transformations
, testProperty "map" pMap
-- Folds
Expand Down