Module

Control.Coroutine

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

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

type Process = Co Identity

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

#loop

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

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

Run a Process to completion.

#fuseWith

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

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

data Emit o a

A generating functor for emitting output values.

Constructors

Instances

#Producer

type Producer o = Co (Emit o)

A type synonym for a Coroutine which only emits values.

#emit

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

Emit an output value.

#producer

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

newtype Await i a

A generating functor for awaiting input values.

Constructors

Instances

#Consumer

type Consumer i = Co (Await i)

A type synonym for a Coroutine which only awaits values.

#await

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

Await an input value.

#consumer

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

newtype Transform i o a

A generating functor for transforming input values into output values.

Constructors

Instances

#Transformer

type Transformer i o = Co (Transform i o)

A type synonym for a Coroutine which transforms values.

#transform

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

Transform input values.

#CoTransform

data CoTransform i o a

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

Constructors

Instances

#CoTransformer

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

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

Cotransform input values.

#connect

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.

#($$)

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

#pullFrom

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

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.

#($~)

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

#transformConsumer

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.

#(~$)

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

#composeTransformers

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

#(~~)

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

#composeCoTransformers

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

#fuseCoTransform

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.

#transformCoTransformL

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

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.

#joinProducers

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.

#(/\)

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

#joinConsumers

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

#(\/)

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

Modules