on master help sign in
Add cleanup related notes in fold design
commitbcac95de89cb7495f49a57ae07bbc3d96190e135 authorHarendra Kumar <harendra@composewell.com> dateSun 23 Apr 2023, 22:53 (3 years ago) parentc77e8ff
1 file changed+19
core/src/Streamly/Internal/Data/Fold/Type.hs+19
core/src/Streamly/Internal/Data/Fold/Type.hs+19
@@ -126,6 +126,25 @@
126126-- or to not be able to use `take 0` if we have it. Also, applicative and
127127-- monadic composition of folds would not be possible.
128128--
129+-- == Cleanup Action
130+--
131+-- Fold may use other folds in the downstream pipeline. When a fold is done and
132+-- it wants to terminate it needs to wait for the downstream folds before it
133+-- returns. For example, if the downstream fold is an async fold we need to
134+-- wait for the async fold to finish and return the final result.
135+--
136+-- To be able to support this use case we need a cleanup action in the fold.
137+-- The fold gets finalized once the cleanup is called and we can use extract to
138+-- get the final state/result of the fold.
139+--
140+-- Similar to folds we may have a cleanup action in streams as well. Currently,
141+-- we rely on GC to cleanup the streams, if we use a cleanup action then we can
142+-- perform cleanup quickly. Also, similar to folds we can also have an
143+-- "initial" action in streams as well to generate the initial state. It could
144+-- decouple the initialization of the stream from the first element being
145+-- pulled. For example, you may want to start a timer at initialization rather
146+-- than at the first element pull of the stream.
147+--
129148-- == Terminating Folds with backtracking
130149--
131150-- Consider the example of @takeWhile@ operation, it needs to inspect an