diff --git a/CHANGES.md b/CHANGES.md index 1b1148f3..5dfe1e3e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## next + + * Add `HashMap.findWithDefault` (deprecates `HashMap.lookupDefault`) + ## 0.2.10.0 * Add `HashMap.alterF`. diff --git a/Data/HashMap/Base.hs b/Data/HashMap/Base.hs index be65223d..11e28033 100644 --- a/Data/HashMap/Base.hs +++ b/Data/HashMap/Base.hs @@ -25,6 +25,7 @@ module Data.HashMap.Base , size , member , lookup + , findWithDefault , lookupDefault , (!) , insert @@ -624,13 +625,27 @@ lookupCont absent present !h0 !k0 !m0 = go h0 k0 0 m0 -- | /O(log n)/ Return the value to which the specified key is mapped, -- or the default value if this map contains no mapping for the key. -lookupDefault :: (Eq k, Hashable k) +-- +-- @since 0.2.11 +findWithDefault :: (Eq k, Hashable k) => v -- ^ Default value to return. -> k -> HashMap k v -> v -lookupDefault def k t = case lookup k t of +findWithDefault def k t = case lookup k t of Just v -> v _ -> def -{-# INLINABLE lookupDefault #-} +{-# INLINABLE findWithDefault #-} + + +-- | /O(log n)/ Return the value to which the specified key is mapped, +-- or the default value if this map contains no mapping for the key. +-- +-- DEPRECATED: lookupDefault is deprecated as of version 0.2.10, replaced +-- by 'findWithDefault'. +lookupDefault :: (Eq k, Hashable k) + => v -- ^ Default value to return. + -> k -> HashMap k v -> v +lookupDefault def k t = findWithDefault def k t +{-# INLINE lookupDefault #-} -- | /O(log n)/ Return the value to which the specified key is mapped. -- Calls 'error' if this map contains no mapping for the key. diff --git a/Data/HashMap/Lazy.hs b/Data/HashMap/Lazy.hs index c7306350..dfaac6da 100644 --- a/Data/HashMap/Lazy.hs +++ b/Data/HashMap/Lazy.hs @@ -38,6 +38,7 @@ module Data.HashMap.Lazy , size , member , lookup + , findWithDefault , lookupDefault , (!) , insert diff --git a/Data/HashMap/Strict.hs b/Data/HashMap/Strict.hs index 3af68b7f..27d1266b 100644 --- a/Data/HashMap/Strict.hs +++ b/Data/HashMap/Strict.hs @@ -37,6 +37,7 @@ module Data.HashMap.Strict , size , member , lookup + , findWithDefault , lookupDefault , (!) , insert diff --git a/tests/Strictness.hs b/tests/Strictness.hs index aa7e9796..da6c8c4d 100644 --- a/tests/Strictness.hs +++ b/tests/Strictness.hs @@ -55,6 +55,9 @@ pSingletonValueStrict k = isBottom $ (HM.singleton k (bottom :: Int)) pLookupDefaultKeyStrict :: Int -> HashMap Key Int -> Bool pLookupDefaultKeyStrict def m = isBottom $ HM.lookupDefault def bottom m +pFindWithDefaultKeyStrict :: Int -> HashMap Key Int -> Bool +pFindWithDefaultKeyStrict def m = isBottom $ HM.findWithDefault def bottom m + pAdjustKeyStrict :: (Int -> Int) -> HashMap Key Int -> Bool pAdjustKeyStrict f m = isBottom $ HM.adjust f bottom m @@ -161,6 +164,7 @@ tests = , testProperty "member is key-strict" $ keyStrict HM.member , testProperty "lookup is key-strict" $ keyStrict HM.lookup , testProperty "lookupDefault is key-strict" pLookupDefaultKeyStrict + , testProperty "findWithDefault is key-strict" pFindWithDefaultKeyStrict , testProperty "! is key-strict" $ keyStrict (flip (HM.!)) , testProperty "delete is key-strict" $ keyStrict HM.delete , testProperty "adjust is key-strict" pAdjustKeyStrict