Skip to content

ohmrun/stx_coroutine

Repository files navigation

stx_coroutine

This is a functional, compositional grammar of coroutines because everytime I try and do multi party stuff I paint myself into a corner.

You can use them as an Event/Signal interface, but they're a lot better behaved.

package stx.coroutine.core.term;

enum CoroutineSum<I,O,R,E>{
  Emit(o:O,next:Coroutine<I,O,R,E>);
  Wait(fn:Feed<I,O,R,E>);
  Hold(ft:Held<I,O,R,E>);
  Halt(e:Return<R,E>);
}

Stepwise Streams with input and built in flow control.

I = The type of input arguments
O = The type of intermediate results
R = The type of final results
E = The type of errors.

CoroutineSum describes a set of states of a unidirectional pipe, namely:

Wait: Waiting for some input in order to proceed.
Emit: Emiting or yielding a value, plus a reference to the rest of the Coroutine
Halt: The Coroutine is terminated, either naturally with no result Terminated(Cause.Stop), with a result Production(r:R), or with an error Terminated(Cause.Exit(e:Error<E>)).
Hold: Some asynchronous condition needs be fulfilled to continue. typedef Held<I,O,R,E> = Future<Coroutine<I,O,R,E>>

Why is this useful

Conventional streams are limited in that the control of the rate of the stream is not found in their representation, and has to be bolted on. Coroutine allows windowing a stream in such a way as to be in finer control of intermediate results: both the producer and the consumer side have a measure of control.

Types

Different profiles have different uses. Emiter works like an event system or signal: takes no input, and produces no intermediate output and no final result: Coroutine<Nada,O,Nada,E>

Bridge takes and input, and produces a final result R: Coroutine<I,Nada,R,E>
Derive only produces a final result: Coroutine<Nada,Nada,R,E>
Effect neither takes any input or produces anything, and should be sent to thes scheduler with the run function: Coroutine<Nada,Nada,Nada,E>
Pocket takes an input and produces no kind of output. Coroutine<I,Nada,Nada,Nada,E>
Source produce both maybe many outputs O and maybe a final result R: Coroutine<Nada,Nada,O,R,E>
Tunnel takes one or many inputs I and produces maybe many outputs R before stopping with maybe E: Coroutine<I,O,Nada,E>

About

[In Development]Algebra of coroutines.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages