@@ -7,15 +7,19 @@ module Unison.Util.Map
77    deleteLookup ,
88    deleteLookupJust ,
99    elemsSet ,
10+     foldKeysCommutative ,
11+     foldValuesCommutative ,
1012    foldM ,
1113    foldMapM ,
1214    for_ ,
15+     fromSetA ,
1316    insertLookup ,
1417    invert ,
1518    lookupJust ,
1619    mergeMap ,
1720    unionWithM ,
1821    remap ,
22+     thenInsertPair ,
1923    traverseKeys ,
2024    traverseKeysWith ,
2125    swap ,
@@ -36,6 +40,7 @@ import Data.Map.Internal qualified as Map (Map (Bin, Tip))
3640import  Data.Map.Merge.Strict  qualified  as  Map 
3741import  Data.Map.Strict  qualified  as  Map 
3842import  Data.Set  qualified  as  Set 
43+ import  Data.Set.Internal  qualified  as  Set  (Set  (.. ))
3944import  Data.These  (These  (.. ))
4045import  Data.Vector  (Vector )
4146import  Data.Vector  qualified  as  Vector 
@@ -123,6 +128,26 @@ elemsSet :: (Ord v) => Map k v -> Set v
123128elemsSet = 
124129  Set. fromList .  Map. elems
125130
131+ --  |  Fold the keys of a map strictly with a "commutative" combining function that doesn't receive the elements in any 
132+ --  particular order. 
133+ foldKeysCommutative  ::  (k  ->  acc  ->  acc ) ->  acc  ->  Map  k  v  ->  acc 
134+ foldKeysCommutative f = 
135+   let  go ! acc =  \ case 
136+         Map. Bin  _ k _ l r :  xs ->  go (f k acc) (l :  r :  xs)
137+         Map. Tip  :  xs ->  go acc xs
138+         []  ->  acc
139+    in  \ z xs ->  go z [xs]
140+ 
141+ --  |  Fold the values of a map strictly with a "commutative" combining function that doesn't receive the elements in any 
142+ --  particular order. 
143+ foldValuesCommutative  ::  (v  ->  acc  ->  acc ) ->  acc  ->  Map  k  v  ->  acc 
144+ foldValuesCommutative f = 
145+   let  go ! acc =  \ case 
146+         Map. Bin  _ _ v l r :  xs ->  go (f v acc) (l :  r :  xs)
147+         Map. Tip  :  xs ->  go acc xs
148+         []  ->  acc
149+    in  \ z xs ->  go z [xs]
150+ 
126151--  |  Like 'Map.foldlWithKey'', but with a monadic accumulator. 
127152foldM  ::  (Monad   m ) =>  (acc  ->  k  ->  v  ->  m  acc ) ->  acc  ->  Map  k  v  ->  m  acc 
128153foldM f acc0 = 
@@ -158,6 +183,15 @@ for_ m f =
158183        f k v
159184        go ys
160185
186+ --  |  Like 'Map.fromSet', but in an applicative functor. 
187+ fromSetA  ::  (Applicative   m ) =>  (k  ->  m  a ) ->  Set  k  ->  m  (Map  k  a )
188+ fromSetA f = 
189+   go
190+   where 
191+     go =  \ case 
192+       Set. Tip  ->  pure  Map. Tip
193+       Set. Bin  n k l r ->  (\ v l' r' ->  Map. Bin  n k v l' r') <$>  f k <*>  go l <*>  go r
194+ 
161195unionWithM  :: 
162196  forall  m  k  a . 
163197  (Monad   m , Ord   k ) => 
@@ -182,6 +216,11 @@ remap :: (Ord k1) => ((k0, v0) -> (k1, v1)) -> Map k0 v0 -> Map k1 v1
182216remap f = 
183217  Map. fromList .  map  f .  Map. toList
184218
219+ --  |  Insert a pair in postfix-style. 
220+ thenInsertPair  ::  (Ord   k ) =>  Map  k  v  ->  (k , v ) ->  Map  k  v 
221+ thenInsertPair m (k, v) = 
222+   Map. insert k v m
223+ 
185224traverseKeys  ::  (Applicative   f , Ord   k' ) =>  (k  ->  f  k' ) ->  Map  k  v  ->  f  (Map  k'  v )
186225traverseKeys f =  bitraverse f pure 
187226
0 commit comments