Skip to content

Commit 2d984fe

Browse files
committed
adding update
1 parent 39eb600 commit 2d984fe

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Data/HashMap/Base.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Data.HashMap.Base
2626
, unsafeInsert
2727
, delete
2828
, adjust
29+
, update
2930
, alter
3031

3132
-- * Combine
@@ -575,6 +576,14 @@ adjust f k0 m0 = go h0 k0 0 m0
575576
| otherwise = t
576577
{-# INLINABLE adjust #-}
577578

579+
-- | /O(log n)/ The expression (@'update' f k map@) updates the value @x@ at @k@,
580+
-- (if it is in the map). If (f k x) is @'Nothing', the element is deleted.
581+
-- If it is (@'Just' y), the key k is bound to the new value y.
582+
update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
583+
update f = alter (>>= f)
584+
{-# INLINABLE update #-}
585+
586+
578587
-- | /O(log n)/ The expression (@'alter' f k map@) alters the value @x@ at @k@, or
579588
-- absence thereof. @alter@ can be used to insert, delete, or update a value in a
580589
-- map. In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.

Data/HashMap/Lazy.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Data.HashMap.Lazy
4747
, insertWith
4848
, delete
4949
, adjust
50+
, update
5051
, alter
5152

5253
-- * Combine

Data/HashMap/Strict.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Data.HashMap.Strict
4747
, insertWith
4848
, delete
4949
, adjust
50+
, update
5051
, alter
5152

5253
-- * Combine
@@ -94,7 +95,7 @@ import qualified Data.HashMap.Array as A
9495
import qualified Data.HashMap.Base as HM
9596
import Data.HashMap.Base hiding (
9697
alter, adjust, fromList, fromListWith, insert, insertWith, intersectionWith,
97-
map, mapWithKey, singleton, unionWith)
98+
map, mapWithKey, singleton, update, unionWith)
9899
import Data.HashMap.Unsafe (runST)
99100

100101
-- $strictness
@@ -228,6 +229,13 @@ adjust f k0 m0 = go h0 k0 0 m0
228229
| otherwise = t
229230
{-# INLINABLE adjust #-}
230231

232+
-- | /O(log n)/ The expression (@'update' f k map@) updates the value @x@ at @k@,
233+
-- (if it is in the map). If (f k x) is @'Nothing', the element is deleted.
234+
-- If it is (@'Just' y), the key k is bound to the new value y.
235+
update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
236+
update f = alter (>>= f)
237+
{-# INLINABLE update #-}
238+
231239
-- | /O(log n)/ The expression (@'alter' f k map@) alters the value @x@ at @k@, or
232240
-- absence thereof. @alter@ can be used to insert, delete, or update a value in a
233241
-- map. In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.

tests/HashMapProperties.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ pInsertWith k = M.insertWith (+) k 1 `eq_` HM.insertWith (+) k 1
9898
pAdjust :: Key -> [(Key, Int)] -> Bool
9999
pAdjust k = M.adjust succ k `eq_` HM.adjust succ k
100100

101+
pUpdateAdjust :: Key -> [(Key, Int)] -> Bool
102+
pUpdateAdjust k = M.update (Just . succ) k `eq_` HM.update (Just . succ) k
103+
104+
pUpdateDelete :: Key -> [(Key, Int)] -> Bool
105+
pUpdateDelete k = M.update (const Nothing) k `eq_` HM.update (const Nothing) k
106+
101107
pAlterAdjust :: Key -> [(Key, Int)] -> Bool
102108
pAlterAdjust k = M.alter (fmap succ) k `eq_` HM.alter (fmap succ) k
103109

@@ -218,6 +224,8 @@ tests =
218224
, testProperty "deleteCollision" pDeleteCollision
219225
, testProperty "insertWith" pInsertWith
220226
, testProperty "adjust" pAdjust
227+
, testProperty "updateAdjust" pUpdateAdjust
228+
, testProperty "updateDelete" pUpdateDelete
221229
, testProperty "alterAdjust" pAlterAdjust
222230
, testProperty "alterInsert" pAlterInsert
223231
, testProperty "alterDelete" pAlterDelete

0 commit comments

Comments
 (0)