Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active October 31, 2017 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/d57efd532956dc64f7c611fc5ba2f8e7 to your computer and use it in GitHub Desktop.
Save deque-blog/d57efd532956dc64f7c611fc5ba2f8e7 to your computer and use it in GitHub Desktop.
fold : (Monad m) => (a -> b -> b) -> b -> Sink a m b
fold f = recur where
recur acc = do
ma <- await -- await for more inputs
case ma of
Just x => do -- in case there is a value to process
acc' <- f x acc -- * combine it the current accumulator
recur acc' -- * and recurse with the new value
Nothing => pure acc -- otherwise, return the result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment