File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed
Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change 22
33## 1.3.0.0
44
5+ * Add ` lookupInternMap `
56* Replace ` okeyL ` with ` toOKey `
67
78## 1.2.4.1
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ module Data.MapExtras (
2828 extractKeysSmallSet ,
2929 fromKeys ,
3030 fromElems ,
31+ lookupInternMap ,
3132) where
3233
3334import Data.Foldable (toList )
@@ -270,3 +271,16 @@ fromElems f vs =
270271 -- a nice optimization for already sorted keys and with list fusion there should be no overhead
271272 Map. fromList [(f v, v) | v <- toList vs]
272273{-# INLINE fromElems #-}
274+
275+ -- | Look up a key in a map and return the interned key together with its value, if present.
276+ -- The returned key is exactly the one stored in the map.
277+ -- Useful for maximizing sharing by avoiding duplicate-but-equal keys.
278+ lookupInternMap :: Ord k => k -> Map k v -> Maybe (k , v )
279+ lookupInternMap k = go
280+ where
281+ go Tip = Nothing
282+ go (Bin _ kx v l r) =
283+ case compare k kx of
284+ LT -> go l
285+ GT -> go r
286+ EQ -> Just (kx, v)
You can’t perform that action at this time.
0 commit comments