streamly icon indicating copy to clipboard operation
streamly copied to clipboard

Add Continue constructor to the Fold type

Open harendra-kumar opened this issue 2 years ago • 2 comments

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.

harendra-kumar avatar Mar 11 '22 08:03 harendra-kumar

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:

  1. Extend folds to emit output at each step (Partial s b) and use a Discard (similar to Continue) 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 a Stop s constructor as well to accommodate the empty stream in initial of the fold and to be able to extract the value from state if the fold stops without an output.
  2. 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. Can foldMany be as optimal as a scan as described below?
  3. Use a separate Scan abstraction that will use the Discard and Partial s b constructors to represent stream transformations. Scan would act as a special case of the more powerful Pipe 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.

harendra-kumar avatar Mar 21 '22 07:03 harendra-kumar

See also #1533

harendra-kumar avatar Mar 21 '22 19:03 harendra-kumar