Skip to content

Commit bcac95d

Browse files
Add cleanup related notes in fold design
1 parent c77e8ff commit bcac95d

File tree

1 file changed

+19
-0
lines changed
  • core/src/Streamly/Internal/Data/Fold

1 file changed

+19
-0
lines changed

core/src/Streamly/Internal/Data/Fold/Type.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)