Skip to content

Commit 05ec392

Browse files
committed
Strict versions of mapMaybe*
1 parent d5fbab7 commit 05ec392

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Data/HashMap/Base.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module Data.HashMap.Base
8484
, update16M
8585
, update16With'
8686
, updateOrConcatWith
87+
, filterMapAux
8788
) where
8889

8990
#if __GLASGOW_HASKELL__ >= 709
@@ -884,6 +885,7 @@ filterWithKey pred = filterMapAux onLeaf onColl
884885

885886
onColl el@(L k v) | pred k v = Just el
886887
onColl _ = Nothing
888+
{-# INLINE filterWithKey #-}
887889

888890

889891
-- | Common implementation for 'filterWithKey' and 'mapMaybeWithKey',
@@ -953,7 +955,7 @@ filterMapAux onLeaf onColl = go
953955
| Just el <- onColl (A.index ary i)
954956
= A.write mary j el >> step ary mary (i+1) (j+1) n
955957
| otherwise = step ary mary (i+1) j n
956-
{-# INLINE filterWithKey #-}
958+
{-# INLINE filterMapAux #-}
957959

958960
-- | /O(n)/ Filter this map by retaining only elements which values
959961
-- satisfy a predicate.

Data/HashMap/Strict.hs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE BangPatterns, CPP #-}
1+
{-# LANGUAGE BangPatterns, CPP, PatternGuards #-}
22

33
#if __GLASGOW_HASKELL__ >= 702
44
{-# LANGUAGE Trustworthy #-}
@@ -97,7 +97,7 @@ import qualified Data.HashMap.Array as A
9797
import qualified Data.HashMap.Base as HM
9898
import Data.HashMap.Base hiding (
9999
alter, adjust, fromList, fromListWith, insert, insertWith, intersectionWith,
100-
map, mapWithKey, singleton, update, unionWith)
100+
map, mapWithKey, mapMaybe, mapMaybeWithKey, singleton, update, unionWith)
101101
import Data.HashMap.Unsafe (runST)
102102

103103
-- $strictness
@@ -357,6 +357,28 @@ map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2
357357
map f = mapWithKey (const f)
358358
{-# INLINE map #-}
359359

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+
360382
-- TODO: Should we add a strict traverseWithKey?
361383

362384
------------------------------------------------------------------------

0 commit comments

Comments
 (0)