on master help sign in
Add distribute_
83cbe5dHarendra Kumar
src/Streamly/Internal/Data/Fold.hs+17
@@ -146,6 +146,7 @@ module Streamly.Internal.Data.Fold
146
146
147 , tee
147 , tee
148 , distribute
148 , distribute
149+ , distribute_
149
150
150 -- * Partitioning
151 -- * Partitioning
151
152
@@ -866,6 +867,22 @@ distribute :: Monad m => [Fold m a b] -> Fold m a [b]
866distribute [] = foldNil
867distribute [] = foldNil
867distribute (x:xs) = foldCons x (distribute xs)
868distribute (x:xs) = foldCons x (distribute xs)
868
869
870+-- | Like 'distribute' but for folds that return (), this can be more efficient
871+-- than 'distribute' as it does not need to maintain state.
872+--
873+{-# INLINE distribute_ #-}
874+distribute_ :: Monad m => [Fold m a ()] -> Fold m a ()
875+distribute_ fs = Fold step initial extract
876+ where
877+ initial = Prelude.mapM (\(Fold s i e) ->
878+ i >>= \r -> return (Fold s (return r) e)) fs
879+ step ss a = do
880+ Prelude.mapM_ (\(Fold s i _) -> i >>= \r -> s r a >> return ()) ss
881+ return ss
882+ extract ss = do
883+ Prelude.mapM_ (\(Fold _ i e) -> i >>= \r -> e r) ss
884+ return ()
885+
869------------------------------------------------------------------------------
886------------------------------------------------------------------------------
870-- Partitioning
887-- Partitioning
871------------------------------------------------------------------------------
888------------------------------------------------------------------------------