Copyright | (c) 2019 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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
.
Synopsis
- data Refold m c a b = forall s. Refold (s -> a -> m (Step s b)) (c -> m (Step s b)) (s -> m b)
- foldl' :: Monad m => (b -> a -> b) -> Refold m b a b
- sconcat :: (Monad m, Semigroup a) => Refold m a a a
- drainBy :: Monad m => (c -> a -> m b) -> Refold m c a ()
- iterate :: Monad m => Refold m b a b -> Refold m b a b
- lmapM :: Monad m => (a -> m b) -> Refold m c b r -> Refold m c a r
- rmapM :: Monad m => (b -> m c) -> Refold m x a b -> Refold m x a c
- append :: Monad m => Refold m x a b -> Refold m b a b -> Refold m x a b
- take :: Monad m => Int -> Refold m x a b -> Refold m x a b
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