The Fold
type embeds a default initial value, therefore, it is like a
Monoid
whereas the Refold
type has to be supplied with an initial
value, therefore, it is more like a Semigroup
operation.
Refolds can be appended to each other or to a fold to build the fold incrementally. This is useful in incremental builder like use cases.
See the file splitting example in the streamly-examples
repository for an
application of the Refold
type. The Fold
type does not perform as well
in this situation.
Refold
type is to Fold
as Unfold
type is to Stream
. Unfold
provides better optimizaiton than stream in nested operations, similarly,
Refold
provides better optimization than Fold
.
Types
Like Fold
except that the initial state of the accmulator can be
generated using a dynamically supplied input. This affords better stream
fusion optimization in nested fold operations where the initial fold state
is determined based on a dynamic value.
Internal
Constructors
Refolds
Accumulators
sconcat :: (Monad m, Semigroup a) => Refold m a a a Source #
Append the elements of an input stream to a provided starting value.
>>>
stream = fmap Data.Monoid.Sum $ Stream.enumerateFromTo 1 10
>>>
Stream.fold (Fold.fromRefold Refold.sconcat 10) stream
Sum {getSum = 65}
>>>
sconcat = Refold.foldl' (<>)
Internal
iterate :: Monad m => Refold m b a b -> Refold m b a b Source #
Keep running the same consumer over and over again on the input, feeding the output of the previous run to the next.
Internal
Combinators
lmapM :: Monad m => (a -> m b) -> Refold m c b r -> Refold m c a r Source #
lmapM f fold
maps the monadic function f
on the input of the fold.
Internal
rmapM :: Monad m => (b -> m c) -> Refold m x a b -> Refold m x a c Source #
Map a monadic function on the output of a fold.
Internal