Skip to content

Commit 3b1c768

Browse files
authored
Add 'compose' for maps (#299)
1 parent 405859e commit 3b1c768

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Data/HashMap/Internal.hs

+27-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ module Data.HashMap.Internal
6060
, unionWithKey
6161
, unions
6262

63+
-- ** Compose
64+
, compose
65+
6366
-- * Transformations
6467
, map
6568
, mapWithKey
@@ -418,7 +421,7 @@ instance Ord k => Ord1 (HashMap k) where
418421
#endif
419422

420423
-- | The ordering is total and consistent with the `Eq` instance. However,
421-
-- nothing else about the ordering is specified, and it may change from
424+
-- nothing else about the ordering is specified, and it may change from
422425
-- version to version of either this package or of hashable.
423426
instance (Ord k, Ord v) => Ord (HashMap k v) where
424427
compare = cmp compare compare
@@ -1679,6 +1682,29 @@ unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
16791682
unions = L.foldl' union empty
16801683
{-# INLINE unions #-}
16811684

1685+
1686+
------------------------------------------------------------------------
1687+
-- * Compose
1688+
1689+
-- | Relate the keys of one map to the values of
1690+
-- the other, by using the values of the former as keys for lookups
1691+
-- in the latter.
1692+
--
1693+
-- Complexity: \( O (n * \log(m)) \), where \(m\) is the size of the first argument
1694+
--
1695+
-- >>> compose (fromList [('a', "A"), ('b', "B")]) (fromList [(1,'a'),(2,'b'),(3,'z')])
1696+
-- fromList [(1,"A"),(2,"B")]
1697+
--
1698+
-- @
1699+
-- ('compose' bc ab '!?') = (bc '!?') <=< (ab '!?')
1700+
-- @
1701+
--
1702+
-- @since UNRELEASED
1703+
compose :: (Eq b, Hashable b) => HashMap b c -> HashMap a b -> HashMap a c
1704+
compose bc !ab
1705+
| null bc = empty
1706+
| otherwise = mapMaybe (bc !?) ab
1707+
16821708
------------------------------------------------------------------------
16831709
-- * Transformations
16841710

Data/HashMap/Internal/Strict.hs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ module Data.HashMap.Internal.Strict
7474
, unionWithKey
7575
, unions
7676

77+
-- ** Compose
78+
, compose
79+
7780
-- * Transformations
7881
, map
7982
, mapWithKey

Data/HashMap/Lazy.hs

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module Data.HashMap.Lazy
5959
, unionWithKey
6060
, unions
6161

62+
-- ** Compose
63+
, compose
64+
6265
-- * Transformations
6366
, map
6467
, mapWithKey

Data/HashMap/Strict.hs

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ module Data.HashMap.Strict
5858
, unionWithKey
5959
, unions
6060

61+
-- ** Compose
62+
, compose
63+
6164
-- * Transformations
6265
, map
6366
, mapWithKey

0 commit comments

Comments
 (0)