This repository was archived by the owner on Oct 4, 2020. It is now read-only.
File tree 4 files changed +33
-1
lines changed
4 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,17 @@ exports._fmapStrMap = function (m0, f) {
42
42
return m ;
43
43
} ;
44
44
45
+ // jshint maxparams: 2
46
+ exports . _mapWithKey = function ( m0 , f ) {
47
+ var m = { } ;
48
+ for ( var k in m0 ) {
49
+ if ( m0 . hasOwnProperty ( k ) ) {
50
+ m [ k ] = f ( k ) ( m0 [ k ] ) ;
51
+ }
52
+ }
53
+ return m ;
54
+ } ;
55
+
45
56
// jshint maxparams: 1
46
57
exports . _foldM = function ( bind ) {
47
58
return function ( f ) {
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ module Data.StrMap
21
21
, member
22
22
, alter
23
23
, update
24
+ , mapWithKey
24
25
, keys
25
26
, values
26
27
, union
@@ -232,6 +233,12 @@ union m = mutate (\s -> foldM SM.poke s m)
232
233
unions :: forall a . L.List (StrMap a ) -> StrMap a
233
234
unions = foldl union empty
234
235
236
+ foreign import _mapWithKey :: forall a b . Fn2 (StrMap a ) (String -> a -> b ) (StrMap b )
237
+
238
+ -- | Apply a function of two arguments to each key/value pair, producing a new map
239
+ mapWithKey :: forall a b . (String -> a -> b ) -> StrMap a -> StrMap b
240
+ mapWithKey f m = runFn2 _mapWithKey m f
241
+
235
242
instance semigroupStrMap :: (Semigroup a ) => Semigroup (StrMap a ) where
236
243
append m1 m2 = mutate (\s1 -> foldM (\s2 k v2 -> SM .poke s2 k (runFn4 _lookup v2 (\v1 -> v1 <> v2) k m2)) s1 m1) m2
237
244
Original file line number Diff line number Diff line change @@ -301,3 +301,10 @@ mapTests = do
301
301
quickCheck $ \(TestMap m) -> case M .findMax (smallKeyToNumberMap m) of
302
302
Nothing -> M .isEmpty m
303
303
Just { key: k, value: v } -> M .lookup k m == Just v && all (_ <= k) (M .keys m)
304
+
305
+ log " mapWithKey is correct"
306
+ quickCheck $ \(TestMap m :: TestMap String Int ) -> let
307
+ f k v = k <> show v
308
+ resultViaMapWithKey = m # M .mapWithKey f
309
+ resultViaLists = m # M .toList # map (\(Tuple k v) → Tuple k (f k v)) # M .fromFoldable
310
+ in resultViaMapWithKey === resultViaLists
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ import Data.Tuple (Tuple(..), fst)
17
17
18
18
import Partial.Unsafe (unsafePartial )
19
19
20
- import Test.QuickCheck ((<?>), quickCheck , quickCheck' )
20
+ import Test.QuickCheck ((<?>), quickCheck , quickCheck' , (===) )
21
21
import Test.QuickCheck.Arbitrary (class Arbitrary , arbitrary )
22
22
23
23
newtype TestStrMap v = TestStrMap (M.StrMap v )
@@ -153,6 +153,13 @@ strMapTests = do
153
153
log " fromFoldable = zip keys values"
154
154
quickCheck $ \(TestStrMap m) -> M .toList m == zipWith Tuple (fromFoldable $ M .keys m) (M .values m :: List Int )
155
155
156
+ log " mapWithKey is correct"
157
+ quickCheck $ \(TestStrMap m :: TestStrMap Int ) -> let
158
+ f k v = k <> show v
159
+ resultViaMapWithKey = m # M .mapWithKey f
160
+ resultViaLists = m # M .toList # map (\(Tuple k v) → Tuple k (f k v)) # M .fromFoldable
161
+ in resultViaMapWithKey === resultViaLists
162
+
156
163
log " Bug #63: accidental observable mutation in foldMap"
157
164
quickCheck \(TestStrMap m) ->
158
165
let lhs = go m
You can’t perform that action at this time.
0 commit comments