@@ -102,7 +102,7 @@ module Streamly.Internal.Data.Fold
102 -- , ldeleteBy
102 -- , ldeleteBy
103 -- , luniq
103 -- , luniq
104 , lcatMaybes
104 , lcatMaybes
107 -- ** Mapping Filters
107 -- ** Mapping Filters
108 , lmapMaybe
108 , lmapMaybe
@@ -208,6 +208,7 @@ import Control.Monad.IO.Class (MonadIO(..))
208import Data.Functor.Identity (Identity(..))
208import Data.Functor.Identity (Identity(..))
209import Data.Int (Int64)
209import Data.Int (Int64)
210import Data.Map.Strict (Map)
210import Data.Map.Strict (Map)
211+import Data.Maybe (isJust, fromJust)
211#if __GLASGOW_HASKELL__ < 804
212#if __GLASGOW_HASKELL__ < 804
212import Data.Semigroup (Semigroup((<>)))
213import Data.Semigroup (Semigroup((<>)))
@@ -314,6 +315,16 @@ sequence (Fold step initial extract) = Fold step initial extract'
314mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
315mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
315mapM f = sequence . fmap f
316mapM f = sequence . fmap f
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)
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
317------------------------------------------------------------------------------
328------------------------------------------------------------------------------
318-- Transformations on fold inputs
329-- Transformations on fold inputs
319------------------------------------------------------------------------------
330------------------------------------------------------------------------------