Package

purescript-coroutines

Repository
paf31/purescript-coroutines
License
MIT
Uploaded by
paf31

Latest release Build Status Maintainer: paf31 Pursuit

Usage

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 f is the Emit o functor, the coroutine emits an output of type o
  • When f is the Await i functor, the coroutine waits for an input of type i
  • When f is the Transform i o functor, the coroutine waits for an input of type i, and emits an output of type o

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)