Module

Control.Coroutine

Package
purescript-coroutines
Repository
purescript-contrib/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 a m f. Functor f => Monad m => Co f m (Maybe a) -> Co f m a

Loop until the computation returns a Just.

#runProcess Source

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

Run a Process to completion.

#fuseWith Source

fuseWith :: forall par a m h g f. Functor f => Functor g => Functor h => MonadRec m => Parallel par m => (forall d c b. (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 a m h g f. Functor f => Functor g => Functor h => MonadRec m => (forall d c b. (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 o m. Monad m => o -> Producer o m Unit

Emit an output value.

#producer Source

producer :: forall r m o. 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 i m. Monad m => Consumer i m i

Await an input value.

#consumer Source

consumer :: forall r m i. 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 o i m. 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 o i m. Monad m => o -> CoTransformer i o m i

Cotransform input values.

#connect Source

connect :: forall a m f o. 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 a m o. 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 a m f o i. 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 a m f o i. 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 a m f k j i. 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 a m f k j i. MonadRec m => Parallel f m => CoTransformer i j m a -> CoTransformer j k m a -> CoTransformer i k m a

Compose cotransformers

#fuseCoTransform Source

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

Fuse a transformer and a cotransformer.

#transformCoTransformL Source

transformCoTransformL :: forall a m f o i2 i1. 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 a m f o2 o1 i. 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.

#joinProducers Source

joinProducers :: forall a m f o2 o1. 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 a m f i2 i1. 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)