|
1 |
| -{-# LANGUAGE BangPatterns, CPP #-} |
| 1 | +{-# LANGUAGE BangPatterns, CPP, PatternGuards #-} |
2 | 2 |
|
3 | 3 | #if __GLASGOW_HASKELL__ >= 702
|
4 | 4 | {-# LANGUAGE Trustworthy #-}
|
@@ -97,7 +97,7 @@ import qualified Data.HashMap.Array as A
|
97 | 97 | import qualified Data.HashMap.Base as HM
|
98 | 98 | import Data.HashMap.Base hiding (
|
99 | 99 | alter, adjust, fromList, fromListWith, insert, insertWith, intersectionWith,
|
100 |
| - map, mapWithKey, singleton, update, unionWith) |
| 100 | + map, mapWithKey, mapMaybe, mapMaybeWithKey, singleton, update, unionWith) |
101 | 101 | import Data.HashMap.Unsafe (runST)
|
102 | 102 |
|
103 | 103 | -- $strictness
|
@@ -357,6 +357,28 @@ map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2
|
357 | 357 | map f = mapWithKey (const f)
|
358 | 358 | {-# INLINE map #-}
|
359 | 359 |
|
| 360 | + |
| 361 | +------------------------------------------------------------------------ |
| 362 | +-- * Filter |
| 363 | + |
| 364 | +-- | /O(n)/ Transform this map by applying a function to every value |
| 365 | +-- and retaining only some of them. |
| 366 | +mapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2 |
| 367 | +mapMaybeWithKey f = filterMapAux onLeaf onColl |
| 368 | + where onLeaf (Leaf h (L k v)) | Just v' <- f k v = Just (leaf h k v') |
| 369 | + onLeaf _ = Nothing |
| 370 | + |
| 371 | + onColl (L k v) | Just v' <- f k v = Just (L k v') |
| 372 | + | otherwise = Nothing |
| 373 | +{-# INLINE mapMaybeWithKey #-} |
| 374 | + |
| 375 | +-- | /O(n)/ Transform this map by applying a function to every value |
| 376 | +-- and retaining only some of them. |
| 377 | +mapMaybe :: (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2 |
| 378 | +mapMaybe f = mapMaybeWithKey (const f) |
| 379 | +{-# INLINE mapMaybe #-} |
| 380 | + |
| 381 | + |
360 | 382 | -- TODO: Should we add a strict traverseWithKey?
|
361 | 383 |
|
362 | 384 | ------------------------------------------------------------------------
|
|
0 commit comments