This library can be installed using Bower:
bower i purescript-coroutines
and built using Pulp:
pulp build
pulp test
The basic building block is the coroutine type Co, which exhibits different behavior when it suspends based on a functor f:
- When
fis theEmit ofunctor, the coroutine emits an output of typeo - When
fis theAwait ifunctor, the coroutine waits for an input of typei - When
fis theTransform i ofunctor, the coroutine waits for an input of typei, and emits an output of typeo
See the article "Coroutine Pipelines" in The Monad Reader, Issue 19 for more details.
Using these building blocks, we can create some standard coroutines, which can form pipelines.
Here is a coroutine which generates the natural numbers in order:
nats :: forall m. (Monad m) => Producer Int m Unit
nats = go 0
where
go i = do
emit i
go (i + 1)Here is a coroutine which accepts and prints strings:
printer :: forall a. Consumer String (Aff _) Unit
printer = forever do
s <- await
lift (log s)Here is a coroutine which transforms inputs by showing them as strings:
showing :: forall a m. (Show a, Monad m) => Transformer a String m Unit
showing = forever (transform show)These coroutines can be combined using a handful of operators, and run using runFreeT or runProcess:
main = launchAff $ runProcess ((nats $~ showing) $$ printer)