streamly
streamly copied to clipboard
Add Continue constructor to the Fold type
This will help in representing filtering scans using folds. Currently folds cannot skip producing output, to skip we output we have to output Just or Nothing. A Continue constructor would represent the Nothing state.
The problem to solve is how to compose such that same filters can be used with both streams and folds. There are multiple design choices:
- Extend folds to emit output at each step (
Partial s b
) and use aDiscard
(similar toContinue
) constructor to discard an input. This makes folds represent filtering scans as well. But this complicates folds in multiple ways: folds now always return a Maybe value to accommodate the empty stream case for scans. We need aStop s
constructor as well to accommodate the empty stream ininitial
of the fold and to be able to extract the value from state if the fold stops without an output. - Implement filters as Maybe folds and then scan the stream with it discarding the Nothing values, we can use a special filtering combinator to do that. We can use
foldMany
on Maybe folds. CanfoldMany
be as optimal as a scan as described below? - Use a separate Scan abstraction that will use the
Discard
andPartial s b
constructors to represent stream transformations. Scan would act as a special case of the more powerfulPipe
abstraction. This will keep the fold abstraction simpler that would consume input until it generates an output whereas scan repesents only emitting intermediate steps. We can still scan streams using folds using (2) above, folds in fact represent a more powerful way to scan.
See also #1533