This repository was archived by the owner on Oct 4, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +33
-1
lines changed Expand file tree Collapse file tree 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) {
4242 return m ;
4343} ;
4444
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+
4556// jshint maxparams: 1
4657exports . _foldM = function ( bind ) {
4758 return function ( f ) {
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ module Data.StrMap
2121 , member
2222 , alter
2323 , update
24+ , mapWithKey
2425 , keys
2526 , values
2627 , union
@@ -232,6 +233,12 @@ union m = mutate (\s -> foldM SM.poke s m)
232233unions :: forall a . L.List (StrMap a ) -> StrMap a
233234unions = foldl union empty
234235
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+
235242instance semigroupStrMap :: (Semigroup a ) => Semigroup (StrMap a ) where
236243 append m1 m2 = mutate (\s1 -> foldM (\s2 k v2 -> SM .poke s2 k (runFn4 _lookup v2 (\v1 -> v1 <> v2) k m2)) s1 m1) m2
237244
Original file line number Diff line number Diff line change @@ -301,3 +301,10 @@ mapTests = do
301301 quickCheck $ \(TestMap m) -> case M .findMax (smallKeyToNumberMap m) of
302302 Nothing -> M .isEmpty m
303303 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)
1717
1818import Partial.Unsafe (unsafePartial )
1919
20- import Test.QuickCheck ((<?>), quickCheck , quickCheck' )
20+ import Test.QuickCheck ((<?>), quickCheck , quickCheck' , (===) )
2121import Test.QuickCheck.Arbitrary (class Arbitrary , arbitrary )
2222
2323newtype TestStrMap v = TestStrMap (M.StrMap v )
@@ -153,6 +153,13 @@ strMapTests = do
153153 log " fromFoldable = zip keys values"
154154 quickCheck $ \(TestStrMap m) -> M .toList m == zipWith Tuple (fromFoldable $ M .keys m) (M .values m :: List Int )
155155
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+
156163 log " Bug #63: accidental observable mutation in foldMap"
157164 quickCheck \(TestStrMap m) ->
158165 let lhs = go m
You can’t perform that action at this time.
0 commit comments