@@ -102,7 +102,7 @@ module Streamly.Internal.Data.Fold
102102 -- , ldeleteBy
103103 -- , luniq
104104 , lcatMaybes
105−
105+ , mapMaybe
106106 {-
107107 -- ** Mapping Filters
108108 , lmapMaybe
@@ -208,6 +208,7 @@ import Control.Monad.IO.Class (MonadIO(..))
208208import Data.Functor.Identity (Identity(..))
209209import Data.Int (Int64)
210210import Data.Map.Strict (Map)
211+import Data.Maybe (isJust, fromJust)
211212#if __GLASGOW_HASKELL__ < 804
212213import Data.Semigroup (Semigroup((<>)))
213214#endif
@@ -314,6 +315,16 @@ sequence (Fold step initial extract) = Fold step initial extract'
314315mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
315316mapM f = sequence . fmap f
316317
318+-- | @(mapMaybe f fold)@ maps a 'Maybe' returning function @f@ on the input of the fold,
319+-- filter out the 'Nothing' elements, and return a values extracted from 'Just'.
320+-- >>> S.fold (FL.mapMaybe (\x -> case x of 5 -> Just x ; _ -> Nothing) FL.sum)
321+-- (S.enumerateFromTo 1 10)
322+-- 5
323+-- /Internal/
324+{-# INLINE mapMaybe #-}
325+mapMaybe :: (Monad m) => (a -> Maybe b) -> Fold m b r -> Fold m a r
326+mapMaybe f = lmap f . lfilter isJust . lmap fromJust
327+
317328------------------------------------------------------------------------------
318329-- Transformations on fold inputs
319330------------------------------------------------------------------------------