snoyberg/conduit — pull request #90 by ppetr (2013-03-06 19:29:37+00:00)
Title: A suggestion: Generalized `await...` convenience functions.

They allow iterating an input with the possibility of early termination and threading an intermediate value through the computations.

`awaitFold` somewhat generalizes `avaitForever`: `awaitFold` with `r = ()` is just `awaitForever`.

Other variants `awaitWhile` and `awaitFoldE` generalize them in other way - they allow early termination of a loop.

----

The example from [Multiple Conduits](https://www.fpcomplete.com/user/snoyberg/random-code-snippets/multiple-conduits) can be then written as

accumer :: Monad m => Conduit Int m Int
accumer =
loop 0
where
loop = awaitFold $ \accum i -> do
let accum' = accum + i
yield accum'
return accum'

breaker :: Monad m => Conduit Int m Int -> Conduit Int m Int
breaker inner = do
...
where
go = awaitWhile $ \i -> yield i >> return (i `mod` 5 /= 0)