@@ -60,6 +60,9 @@ module Data.HashMap.Internal
60
60
, unionWithKey
61
61
, unions
62
62
63
+ -- ** Compose
64
+ , compose
65
+
63
66
-- * Transformations
64
67
, map
65
68
, mapWithKey
@@ -418,7 +421,7 @@ instance Ord k => Ord1 (HashMap k) where
418
421
#endif
419
422
420
423
-- | 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
422
425
-- version to version of either this package or of hashable.
423
426
instance (Ord k , Ord v ) => Ord (HashMap k v ) where
424
427
compare = cmp compare compare
@@ -1679,6 +1682,29 @@ unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
1679
1682
unions = L. foldl' union empty
1680
1683
{-# INLINE unions #-}
1681
1684
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
+
1682
1708
------------------------------------------------------------------------
1683
1709
-- * Transformations
1684
1710
0 commit comments