Skip to content

Commit 6226dc0

Browse files
Javranphadej
authored andcommitted
Implement KeyMap.!? operator.
This is to match the counterpart in HashMap, making it smoother migration to aeson-2+
1 parent 620e2d6 commit 6226dc0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Data/Aeson/KeyMap.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Data.Aeson.KeyMap (
1616
-- * Query
1717
null,
1818
lookup,
19+
(!?),
1920
size,
2021
member,
2122

@@ -184,6 +185,14 @@ alterF f k m = fmap g (f mv) where
184185
lookup :: Key -> KeyMap v -> Maybe v
185186
lookup t tm = M.lookup t (unKeyMap tm)
186187

188+
-- | Return the value to which the specified key is mapped,
189+
-- or Nothing if this map contains no mapping for the key.
190+
--
191+
-- This is a flipped version of 'lookup'.
192+
--
193+
(!?) :: KeyMap v -> Key -> Maybe v
194+
(!?) m k = lookup k m
195+
187196
-- | Associate the specified value with the specified key
188197
-- in this map. If this map previously contained a mapping
189198
-- for the key, the old value is replaced.
@@ -387,6 +396,14 @@ alterF f k = fmap KeyMap . H.alterF f k . unKeyMap
387396
lookup :: Key -> KeyMap v -> Maybe v
388397
lookup t tm = H.lookup t (unKeyMap tm)
389398

399+
-- | Return the value to which the specified key is mapped,
400+
-- or Nothing if this map contains no mapping for the key.
401+
--
402+
-- This is a flipped version of 'lookup'.
403+
--
404+
(!?) :: KeyMap v -> Key -> Maybe v
405+
(!?) m k = lookup k m
406+
390407
-- | Associate the specified value with the specified key
391408
-- in this map. If this map previously contained a mapping
392409
-- for the key, the old value is replaced.

0 commit comments

Comments
 (0)