Module

Control.Coroutine

Package
purescript-coroutines
Repository
paf31/purescript-coroutines

This module defines types and functions for working with coroutines. Coroutines are defined based on some underlying functor, which means that the same machinery can be used for coroutines which emit values, await values, fork, join, or any combination.

#Co Source

type Co = FreeT

A coroutine whose commands are given by the functor f, with side effects at each step given by the monad m.

#Process Source

type Process = Co Identity

A Process is a Coroutine which only has side effects, and supports no commands.

#loop Source

loop :: forall f m a. Functor f => Monad m => Co f m (Maybe a) -> Co f m a

Loop until the computation returns a Just.

#runProcess Source

runProcess :: forall m a. MonadRec m => Process m a -> m a

Run a Process to completion.

#fuseWith Source

fuseWith :: forall f g h m a par. Functor f => Functor g => Functor h => MonadRec m => Parallel par m => (forall b c d. (b -> c -> d) -> f b -> g c -> h d) -> Co f m a -> Co g m a -> Co h m a

Fuse two Coroutines.

#fuseWithL Source

fuseWithL :: forall f g h m a. Functor f => Functor g => Functor h => MonadRec m => (forall b c d. (b -> c -> d) -> f b -> g c -> h d) -> Co f m a -> Co g m a -> Co h m a

Fuse two Coroutines with a bias to the left.

#Emit Source

data Emit o a

A generating functor for emitting output values.

Constructors

Instances

#Producer Source

type Producer o = Co (Emit o)

A type synonym for a Coroutine which only emits values.

#emit Source

emit :: forall m o. Monad m => o -> Producer o m Unit

Emit an output value.

#producer Source

producer :: forall o m r. Monad m => m (Either o r) -> Producer o m r

Create a Producer by providing a monadic function that produces values.

The function should return a value of type r at most once, when the Producer is ready to close.

#Await Source

newtype Await i a

A generating functor for awaiting input values.

Constructors

Instances

#Consumer Source

type Consumer i = Co (Await i)

A type synonym for a Coroutine which only awaits values.

#await Source

await :: forall m i. Monad m => Consumer i m i

Await an input value.

#consumer Source

consumer :: forall i m r. Monad m => (i -> m (Maybe r)) -> Consumer i m r

Create a Consumer by providing a handler function which consumes values.

The handler function should return a value of type r at most once, when the Consumer is ready to close.

#Transform Source

newtype Transform i o a

A generating functor for transforming input values into output values.

Constructors

Instances

#Transformer Source

type Transformer i o = Co (Transform i o)

A type synonym for a Coroutine which transforms values.

#transform Source

transform :: forall m i o. Monad m => (i -> o) -> Transformer i o m Unit

Transform input values.

#CoTransform Source

data CoTransform i o a

A generating functor which yields a value before waiting for an input.

Constructors

Instances

#CoTransformer Source

type CoTransformer i o = Co (CoTransform i o)

A type synonym for a Coroutine which "cotransforms" values, emitting an output before waiting for its input.

#cotransform Source

cotransform :: forall m i o. Monad m => o -> CoTransformer i o m i

Cotransform input values.

#transformCoTransformL Source

transformCoTransformL :: forall i1 i2 o f m a. MonadRec m => Parallel f m => Transformer i1 i2 m a -> CoTransformer i2 o m a -> CoTransformer i1 o m a

Transform a CoTransformer on the left.

#transformCoTransformR Source

transformCoTransformR :: forall i o1 o2 f m a. MonadRec m => Parallel f m => CoTransformer i o1 m a -> Transformer o1 o2 m a -> CoTransformer i o2 m a

Transform a CoTransformer on the right.

#fuseCoTransform Source

fuseCoTransform :: forall i o f m a. MonadRec m => Parallel f m => Transformer i o m a -> CoTransformer o i m a -> Process m a

Fuse a transformer and a cotransformer.

#connect Source

connect :: forall o f m a. MonadRec m => Parallel f m => Producer o m a -> Consumer o m a -> Process m a

Connect a producer and a consumer.

#($$) Source

Operator alias for Control.Coroutine.connect (right-associative / precedence 2)

#pullFrom Source

pullFrom :: forall o m a. MonadRec m => Consumer o m a -> Producer o m a -> Process m a

Connect a producer and a consumer so that the consumer pulls from the producer. This means the process ends immediately when the consumer closes.

#transformProducer Source

transformProducer :: forall i o f m a. MonadRec m => Parallel f m => Producer i m a -> Transformer i o m a -> Producer o m a

Transform a producer.

#($~) Source

Operator alias for Control.Coroutine.transformProducer (right-associative / precedence 2)

#transformConsumer Source

transformConsumer :: forall i o f m a. MonadRec m => Parallel f m => Transformer i o m a -> Consumer o m a -> Consumer i m a

Transform a consumer.

#(~$) Source

Operator alias for Control.Coroutine.transformConsumer (right-associative / precedence 2)

#composeTransformers Source

composeTransformers :: forall i j k f m a. MonadRec m => Parallel f m => Transformer i j m a -> Transformer j k m a -> Transformer i k m a

Compose transformers

#(~~) Source

Operator alias for Control.Coroutine.composeTransformers (right-associative / precedence 2)

#composeCoTransformers Source

composeCoTransformers :: forall i j k f m a. MonadRec m => Parallel f m => CoTransformer i j m a -> CoTransformer j k m a -> CoTransformer i k m a

Compose cotransformers

#joinProducers Source

joinProducers :: forall o1 o2 f m a. MonadRec m => Parallel f m => Producer o1 m a -> Producer o2 m a -> Producer (Tuple o1 o2) m a

Run two producers together.

#(/\) Source

Operator alias for Control.Coroutine.joinProducers (right-associative / precedence 3)

#joinConsumers Source

joinConsumers :: forall i1 i2 f m a. MonadRec m => Parallel f m => Consumer i1 m a -> Consumer i2 m a -> Consumer (Tuple i1 i2) m a

Run two consumers together

#(\/) Source

Operator alias for Control.Coroutine.joinConsumers (right-associative / precedence 3)