on master help sign in
Add foldlM1' to Data.Fold
cfafb12Harendra Kumar
core/src/Streamly/Data/Fold.hs+1
@@ -137,6 +137,7 @@ module Streamly.Data.Fold
137 , foldl'
137 , foldl'
138 , foldlM'
138 , foldlM'
139 , foldl1'
139 , foldl1'
140+ , foldlM1'
140 , foldr'
141 , foldr'
141
142
142 -- * Folds
143 -- * Folds
core/src/Streamly/Internal/Data/Fold.hs+1
@@ -31,6 +31,7 @@ module Streamly.Internal.Data.Fold
31 , foldl'
31 , foldl'
32 , foldlM'
32 , foldlM'
33 , foldl1'
33 , foldl1'
34+ , foldlM1'
34 , foldt'
35 , foldt'
35 , foldtM'
36 , foldtM'
36 , foldr'
37 , foldr'
core/src/Streamly/Internal/Data/Fold/Type.hs+13
@@ -335,6 +335,7 @@ module Streamly.Internal.Data.Fold.Type
335 , foldl'
335 , foldl'
336 , foldlM'
336 , foldlM'
337 , foldl1'
337 , foldl1'
338+ , foldlM1'
338 , foldt'
339 , foldt'
339 , foldtM'
340 , foldtM'
340 , foldr'
341 , foldr'
@@ -550,6 +551,18 @@ foldl1' step = fmap toMaybe $ foldl' step1 Nothing'
550 step1 Nothing' a = Just' a
551 step1 Nothing' a = Just' a
551 step1 (Just' x) a = Just' $ step x a
552 step1 (Just' x) a = Just' $ step x a
552
553
554+-- | Like 'foldl1\'' but with a monadic step function.
555+--
556+-- /Pre-release/
557+{-# INLINE foldlM1' #-}
558+foldlM1' :: Monad m => (a -> a -> m a) -> Fold m a (Maybe a)
559+foldlM1' step = fmap toMaybe $ foldlM' step1 (return Nothing')
560+
561+ where
562+
563+ step1 Nothing' a = return $ Just' a
564+ step1 (Just' x) a = Just' <$> step x a
565+
553------------------------------------------------------------------------------
566------------------------------------------------------------------------------
554-- Right fold constructors
567-- Right fold constructors
555------------------------------------------------------------------------------
568------------------------------------------------------------------------------