Skip to content
Merged
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
17 changes: 17 additions & 0 deletions Data/HashMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module Data.HashMap.Internal
, map
, mapWithKey
, traverseWithKey
, mapKeys

-- * Difference and intersection
, difference
Expand Down Expand Up @@ -1751,6 +1752,22 @@ traverseWithKey f = go
Collision h <$> A.traverse' (\ (L k v) -> L k <$> f k v) ary
{-# INLINE traverseWithKey #-}

-- | /O(n)/.
-- @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@.
--
-- The size of the result may be smaller if @f@ maps two or more distinct
-- keys to the same new key. In this case there is no guarantee which of the
-- associated values is chosen for the conflicting key.
--
-- >>> mapKeys (+ 1) (fromList [(5,"a"), (3,"b")])
-- fromList [(4,"b"),(6,"a")]
-- >>> mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
-- fromList [(1,"c")]
-- >>> mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
-- fromList [(3,"c")]
mapKeys :: (Eq k2, Hashable k2) => (k1 -> k2) -> HashMap k1 v -> HashMap k2 v
mapKeys f = fromList . foldrWithKey (\k x xs -> (f k, x) : xs) []

------------------------------------------------------------------------
-- * Difference and intersection

Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Internal/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module Data.HashMap.Internal.Strict
, map
, mapWithKey
, traverseWithKey
, mapKeys

-- * Difference and intersection
, difference
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Data.HashMap.Lazy
, map
, mapWithKey
, traverseWithKey
, mapKeys

-- * Difference and intersection
, difference
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module Data.HashMap.Strict
, map
, mapWithKey
, traverseWithKey
, mapKeys

-- * Difference and intersection
, difference
Expand Down
4 changes: 4 additions & 0 deletions tests/HashMapProperties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ pTraverse xs =
L.sort (fmap (L.sort . M.toList) (M.traverseWithKey (\_ v -> [v + 1, v + 2]) (M.fromList (take 10 xs))))
== L.sort (fmap (L.sort . HM.toList) (HM.traverseWithKey (\_ v -> [v + 1, v + 2]) (HM.fromList (take 10 xs))))

pMapKeys :: [(Int, Int)] -> Bool
pMapKeys = M.mapKeys (+1) `eq_` HM.mapKeys (+1)

------------------------------------------------------------------------
-- ** Difference and intersection

Expand Down Expand Up @@ -504,6 +507,7 @@ tests =
-- Transformations
, testProperty "map" pMap
, testProperty "traverse" pTraverse
, testProperty "mapKeys" pMapKeys
-- Folds
, testGroup "folds"
[ testProperty "foldr" pFoldr
Expand Down