@@ -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.
423426instance (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
16791682unions = 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
0 commit comments