@@ -155,7 +155,7 @@ module Streamly.Internal.Data.Fold
156 -- * Running A Fold
156 -- * Running A Fold
158− , driveBreak
158+ , breakStream
160 -- * Building Incrementally
160 -- * Building Incrementally
161 , extractM
161 , extractM
@@ -165,7 +165,9 @@ module Streamly.Internal.Data.Fold
170 -- * Combinators
172 -- * Combinators
171 -- ** Utilities
173 -- ** Utilities
@@ -337,9 +339,11 @@ import Streamly.Internal.Data.Fold.Type
338-- >>> :set -package random
340-- >>> :set -package random
339-- >>> import Control.Monad (void)
341-- >>> import Control.Monad (void)
340−-- >>> import Data.Functor.Identity (runIdentity)
342+-- >>> import Data.Functor.Identity (Identity, runIdentity)
341-- >>> import Data.Maybe (isJust)
343-- >>> import Data.Maybe (isJust)
342-- >>> import Data.Monoid (Last(..), Sum(..))
344-- >>> import Data.Monoid (Last(..), Sum(..))
345+-- >>> import Streamly.Data.Array (Array)
346+-- >>> import Streamly.Data.Fold (Fold)
343-- >>> import Streamly.Data.Stream (Stream)
347-- >>> import Streamly.Data.Stream (Stream)
344-- >>> import Data.IORef (newIORef, readIORef, writeIORef)
348-- >>> import Data.IORef (newIORef, readIORef, writeIORef)
345-- >>> import qualified Streamly.Data.Array as Array
349-- >>> import qualified Streamly.Data.Array as Array
@@ -379,33 +383,82 @@ drive strm fl = StreamD.fold fl $ StreamD.fromStreamK $ Stream.toStreamK strm
380-- Definition:
384-- Definition:
382−-- >>> driveBreak = flip Stream.foldBreak
386+-- >>> breakStream = flip Stream.foldBreak
386−{-# INLINE driveBreak #-}
390+{-# INLINE breakStream #-}
387−driveBreak :: Monad m => Stream m a -> Fold m a b -> m (b, Stream m a)
391+breakStream :: Monad m => Stream m a -> Fold m a b -> m (b, Stream m a)
388−driveBreak strm fl = fmap f $ K.foldBreak fl (Stream.toStreamK strm)
392+breakStream strm fl = fmap f $ K.foldBreak fl (Stream.toStreamK strm)
392 f (b, str) = (b, Stream.fromStreamK str)
396 f (b, str) = (b, Stream.fromStreamK str)
394−-- | Augment a fold strictly by adding a stream to it.
398+-- | Append a stream to a fold to build the fold accumulator incrementally. We
399+-- can repeatedly call 'addStream' on the same fold to continue building the
400+-- fold and finally use 'drive' to finish the fold and extract the result. Also
401+-- see the 'Streamly.Data.Fold.addOne' operation which is a singleton version
396-- Definitions:
404-- Definitions:
398−-- >>> augment stream = Fold.drive stream . Fold.duplicate
406+-- >>> addStream stream = Fold.drive stream . Fold.duplicate
408+-- Example, build a list incrementally:
411+-- pure (Fold.toList :: Fold IO Int [Int])
413+-- >>= Fold.addStream (Stream.enumerateFromTo 2 4)
414+-- >>= Fold.drive Stream.nil
419+-- This can be used as an O(n) list append compared to the O(n^2) @++@ when
420+-- used for incrementally building a list.
422+-- Example, build a stream incrementally:
425+-- pure (Fold.toStream :: Fold IO Int (Stream Identity Int))
427+-- >>= Fold.addStream (Stream.enumerateFromTo 2 4)
428+-- >>= Fold.drive Stream.nil
431+-- fromList [1,2,3,4]
433+-- This can be used as an O(n) stream append compared to the O(n^2) @<>@ when
434+-- used for incrementally building a stream.
436+-- Example, build an array incrementally:
439+-- pure (Array.write :: Fold IO Int (Array Int))
441+-- >>= Fold.addStream (Stream.enumerateFromTo 2 4)
442+-- >>= Fold.drive Stream.nil
445+-- fromList [1,2,3,4]
447+-- Example, build an array stream incrementally:
401−-- Fold.augment (Stream.enumerateFromTo 1 10) Fold.sum
450+-- let f :: Fold IO Int (Stream Identity (Array Int))
451+-- f = Fold.chunksOf 2 (Array.writeN 3) Fold.toStream
402−-- >>= Fold.augment (Stream.enumerateFromTo 11 20)
454+-- >>= Fold.addStream (Stream.enumerateFromTo 2 4)
403−-- >>= Fold.drive (Stream.enumerateFromTo 21 30)
455+-- >>= Fold.drive Stream.nil
405−-- 465
458+-- fromList [fromList [1,2],fromList [3,4]]
407−augment :: Monad m => Stream m a -> Fold m a b -> m (Fold m a b)
460+addStream :: Monad m => Stream m a -> Fold m a b -> m (Fold m a b)
408−augment stream = drive stream . duplicate
461+addStream stream = drive stream . duplicate
410------------------------------------------------------------------------------
463------------------------------------------------------------------------------
411-- Transformations on fold inputs
464-- Transformations on fold inputs