Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 4cd5772

Browse files
authored
Merge pull request #69 from clayrat/master
add mapWithKey
2 parents f831f48 + 8d59376 commit 4cd5772

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/Data/Map.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Data.Map
3232
, unionWith
3333
, unions
3434
, size
35+
, mapWithKey
3536
) where
3637

3738
import Prelude
@@ -410,3 +411,9 @@ unions = foldl union empty
410411
-- | Calculate the number of key/value pairs in a map
411412
size :: forall k v. Map k v -> Int
412413
size = length <<< values
414+
415+
-- | Apply a function of two arguments to each key/value pair, producing a new map
416+
mapWithKey :: forall k v v'. (k -> v -> v') -> Map k v -> Map k v'
417+
mapWithKey _ Leaf = Leaf
418+
mapWithKey f (Two left k v right) = Two (mapWithKey f left) k (f k v) (mapWithKey f right)
419+
mapWithKey f (Three left k1 v1 mid k2 v2 right) = Three (mapWithKey f left) k1 (f k1 v1) (mapWithKey f mid) k2 (f k2 v2) (mapWithKey f right)

0 commit comments

Comments
 (0)