on master help sign in
Add foldlM1' to Data.Fold
commitcfafb121af6075e2b017101342a21ddafb830817 authorHarendra Kumar <harendra@composewell.com> dateMon 6 Mar 2023, 12:08 (4 years ago) parent9e23d29
3 files changed+15
core/src/Streamly/Data/Fold.hs+1 core/src/Streamly/Internal/Data/Fold.hs+1 core/src/Streamly/Internal/Data/Fold/Type.hs+13
core/src/Streamly/Data/Fold.hs+1
@@ -137,6 +137,7 @@ module Streamly.Data.Fold
137137 , foldl'
138138 , foldlM'
139139 , foldl1'
140+ , foldlM1'
140141 , foldr'
141142
142143 -- * Folds
core/src/Streamly/Internal/Data/Fold.hs+1
@@ -31,6 +31,7 @@ module Streamly.Internal.Data.Fold
3131 , foldl'
3232 , foldlM'
3333 , foldl1'
34+ , foldlM1'
3435 , foldt'
3536 , foldtM'
3637 , foldr'
core/src/Streamly/Internal/Data/Fold/Type.hs+13
@@ -335,6 +335,7 @@ module Streamly.Internal.Data.Fold.Type
335335 , foldl'
336336 , foldlM'
337337 , foldl1'
338+ , foldlM1'
338339 , foldt'
339340 , foldtM'
340341 , foldr'
@@ -550,6 +551,18 @@ foldl1' step = fmap toMaybe $ foldl' step1 Nothing'
550551 step1 Nothing' a = Just' a
551552 step1 (Just' x) a = Just' $ step x a
552553
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+
553566------------------------------------------------------------------------------
554567-- Right fold constructors
555568------------------------------------------------------------------------------