Skip to content

Commit

Permalink
Add HashMap.findWithDefault.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-renaud committed Jan 24, 2018
1 parent 60ced06 commit 83c2a3f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Data/HashMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Data.HashMap.Base
, size
, member
, lookup
, findWithDefault
, lookupDefault
, (!)
, insert
Expand Down Expand Up @@ -450,12 +451,24 @@ lookup 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.9
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 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 "As of version 0.2.9, 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
{-# INLINABLE lookupDefault #-}

-- | /O(log n)/ Return the value to which the specified key is mapped.
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Data.HashMap.Lazy
, size
, member
, HM.lookup
, findWithDefault
, lookupDefault
, (!)
, insert
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Data.HashMap.Strict
, size
, HM.member
, HM.lookup
, findWithDefault
, lookupDefault
, (!)
, insert
Expand Down
4 changes: 4 additions & 0 deletions tests/Strictness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 83c2a3f

Please sign in to comment.