Skip to content

Commit da5ac39

Browse files
authored
Add HashMap.findWithDefault (#176)
* Add HashMap.findWithDefault. This function is equivalent to `lookupDefault` but uses the same name that the containers package uses (https://hackage.haskell.org/package/containers-0.5.10.2/docs/Data-Map-Strict.html#v:findWithDefault). This partially addresses #172. * Remove DEPRECATED pragma, only "soft deprecate" in comments. * Update changelog and change @SInCE annotation.
1 parent 042d354 commit da5ac39

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## next
2+
3+
* Add `HashMap.findWithDefault` (deprecates `HashMap.lookupDefault`)
4+
15
## 0.2.10.0
26

37
* Add `HashMap.alterF`.

Data/HashMap/Base.hs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Data.HashMap.Base
2525
, size
2626
, member
2727
, lookup
28+
, findWithDefault
2829
, lookupDefault
2930
, (!)
3031
, insert
@@ -624,13 +625,27 @@ lookupCont absent present !h0 !k0 !m0 = go h0 k0 0 m0
624625

625626
-- | /O(log n)/ Return the value to which the specified key is mapped,
626627
-- or the default value if this map contains no mapping for the key.
627-
lookupDefault :: (Eq k, Hashable k)
628+
--
629+
-- @since 0.2.11
630+
findWithDefault :: (Eq k, Hashable k)
628631
=> v -- ^ Default value to return.
629632
-> k -> HashMap k v -> v
630-
lookupDefault def k t = case lookup k t of
633+
findWithDefault def k t = case lookup k t of
631634
Just v -> v
632635
_ -> def
633-
{-# INLINABLE lookupDefault #-}
636+
{-# INLINABLE findWithDefault #-}
637+
638+
639+
-- | /O(log n)/ Return the value to which the specified key is mapped,
640+
-- or the default value if this map contains no mapping for the key.
641+
--
642+
-- DEPRECATED: lookupDefault is deprecated as of version 0.2.10, replaced
643+
-- by 'findWithDefault'.
644+
lookupDefault :: (Eq k, Hashable k)
645+
=> v -- ^ Default value to return.
646+
-> k -> HashMap k v -> v
647+
lookupDefault def k t = findWithDefault def k t
648+
{-# INLINE lookupDefault #-}
634649

635650
-- | /O(log n)/ Return the value to which the specified key is mapped.
636651
-- Calls 'error' if this map contains no mapping for the key.

Data/HashMap/Lazy.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Data.HashMap.Lazy
3838
, size
3939
, member
4040
, lookup
41+
, findWithDefault
4142
, lookupDefault
4243
, (!)
4344
, insert

Data/HashMap/Strict.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module Data.HashMap.Strict
3737
, size
3838
, member
3939
, lookup
40+
, findWithDefault
4041
, lookupDefault
4142
, (!)
4243
, insert

tests/Strictness.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ pSingletonValueStrict k = isBottom $ (HM.singleton k (bottom :: Int))
5555
pLookupDefaultKeyStrict :: Int -> HashMap Key Int -> Bool
5656
pLookupDefaultKeyStrict def m = isBottom $ HM.lookupDefault def bottom m
5757

58+
pFindWithDefaultKeyStrict :: Int -> HashMap Key Int -> Bool
59+
pFindWithDefaultKeyStrict def m = isBottom $ HM.findWithDefault def bottom m
60+
5861
pAdjustKeyStrict :: (Int -> Int) -> HashMap Key Int -> Bool
5962
pAdjustKeyStrict f m = isBottom $ HM.adjust f bottom m
6063

@@ -161,6 +164,7 @@ tests =
161164
, testProperty "member is key-strict" $ keyStrict HM.member
162165
, testProperty "lookup is key-strict" $ keyStrict HM.lookup
163166
, testProperty "lookupDefault is key-strict" pLookupDefaultKeyStrict
167+
, testProperty "findWithDefault is key-strict" pFindWithDefaultKeyStrict
164168
, testProperty "! is key-strict" $ keyStrict (flip (HM.!))
165169
, testProperty "delete is key-strict" $ keyStrict HM.delete
166170
, testProperty "adjust is key-strict" pAdjustKeyStrict

0 commit comments

Comments
 (0)