Skip to content

Commit ed4e3f5

Browse files
authored
Add operator HashMap.!? (#175)
Add operator HashMap.!?. This is equivalent to `lookup` with the arguments flipped. This operator was added to containers-0.5.9.1, this makes the HashMap interface consistent. This partially addresses #172.
1 parent 4d12c2a commit ed4e3f5

File tree

6 files changed

+21
-0
lines changed

6 files changed

+21
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Add `HashMap.findWithDefault` (deprecates `HashMap.lookupDefault`)
44

5+
* Add `HashMap.!?`, a flipped version of `lookup`.
6+
57
## 0.2.10.0
68

79
* Add `HashMap.alterF`.

Data/HashMap/Base.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Data.HashMap.Base
2525
, size
2626
, member
2727
, lookup
28+
, (!?)
2829
, findWithDefault
2930
, lookupDefault
3031
, (!)
@@ -624,6 +625,17 @@ lookupCont absent present !h0 !k0 !m0 = go h0 k0 0 m0
624625
| otherwise = absent (# #)
625626
{-# INLINE lookupCont #-}
626627

628+
-- | /O(log n)/ Return the value to which the specified key is mapped,
629+
-- or 'Nothing' if this map contains no mapping for the key.
630+
--
631+
-- This is a flipped version of 'lookup'.
632+
--
633+
-- @since 0.2.11
634+
(!?) :: (Eq k, Hashable k) => HashMap k v -> k -> Maybe v
635+
(!?) m k = lookup k m
636+
{-# INLINE (!?) #-}
637+
638+
627639
-- | /O(log n)/ Return the value to which the specified key is mapped,
628640
-- or the default value if this map contains no mapping for the key.
629641
--

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+
, (!?)
4142
, findWithDefault
4243
, lookupDefault
4344
, (!)

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+
, (!?)
4041
, findWithDefault
4142
, lookupDefault
4243
, (!)

Data/HashMap/Strict/Base.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Data.HashMap.Strict.Base
3939
, size
4040
, HM.member
4141
, HM.lookup
42+
, (HM.!?)
4243
, HM.findWithDefault
4344
, lookupDefault
4445
, (!)

tests/HashMapProperties.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ pMember k = M.member k `eq` HM.member k
122122
pLookup :: Key -> [(Key, Int)] -> Bool
123123
pLookup k = M.lookup k `eq` HM.lookup k
124124

125+
pLookupOperator :: Key -> [(Key, Int)] -> Bool
126+
pLookupOperator k = M.lookup k `eq` (HM.!? k)
127+
125128
pInsert :: Key -> Int -> [(Key, Int)] -> Bool
126129
pInsert k v = M.insert k v `eq_` HM.insert k v
127130

@@ -363,6 +366,7 @@ tests =
363366
[ testProperty "size" pSize
364367
, testProperty "member" pMember
365368
, testProperty "lookup" pLookup
369+
, testProperty "!?" pLookupOperator
366370
, testProperty "insert" pInsert
367371
, testProperty "delete" pDelete
368372
, testProperty "deleteCollision" pDeleteCollision

0 commit comments

Comments
 (0)