This repository was archived by the owner on Oct 4, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ module Data.Map
15
15
lookup ,
16
16
toList ,
17
17
fromList ,
18
+ fromListWith ,
18
19
delete ,
19
20
member ,
20
21
alter ,
@@ -228,6 +229,11 @@ toList (Three left k1 v1 mid k2 v2 right) = toList left P.++ [Tuple k1 v1] P.++
228
229
fromList :: forall k v . (P.Ord k ) => [Tuple k v ] -> Map k v
229
230
fromList = foldl (\m (Tuple k v) -> insert k v m) empty
230
231
232
+ fromListWith :: forall k v . (P.Ord k ) => (v -> v -> v ) -> [Tuple k v ] -> Map k v
233
+ fromListWith f = foldl (\m (Tuple k v) -> alter (combine v) k m) empty where
234
+ combine v (Just v') = Just P .$ f v v'
235
+ combine v Nothing = Just v
236
+
231
237
keys :: forall k v . Map k v -> [k ]
232
238
keys Leaf = []
233
239
keys (Two left k _ right) = keys left P .++ [k] P .++ keys right
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ module Data.StrMap
14
14
lookup ,
15
15
toList ,
16
16
fromList ,
17
+ fromListWith ,
17
18
delete ,
18
19
member ,
19
20
alter ,
@@ -255,6 +256,21 @@ fromList l = pureST (do
255
256
for_ l (\(Tuple k v) -> SM .poke s k v)
256
257
P .return s)
257
258
259
+ foreign import _lookupST
260
+ " " "
261
+ function _lookupST(no, yes, k, m) {
262
+ return function() {
263
+ return k in m ? yes(m[k]) : no;
264
+ }
265
+ }
266
+ " " " :: forall a h r z . Fn4 z (a -> z ) String (SM.STStrMap h a ) (Eff (st :: ST.ST h | r ) z )
267
+
268
+ fromListWith :: forall a . (a -> a -> a ) -> [Tuple String a ] -> StrMap a
269
+ fromListWith f l = pureST (do
270
+ s <- SM .new
271
+ for_ l (\(Tuple k v) -> runFn4 _lookupST v (f v) k s P .>>= SM .poke s k)
272
+ P .return s)
273
+
258
274
foreign import _collect
259
275
" " "
260
276
function _collect(f) {
You can’t perform that action at this time.
0 commit comments