Next: , Previous: , Up: Time   [Contents][Index]


4.3.2 Coroutines

(use-modules (sly coroutine))

Coroutines are the building block for cooperative multitasking. When used with agendas, they are a powerful mechanism for writing algorithms that span multiple clock ticks in a straightforward, linear fashion. Sly’s coroutines are built on top of Guile’s delimited continuations, called prompts.

To run a procedure as a coroutine, use the call-with-coroutine procedure. Once inside the coroutine prompt, the yield procedure can be used to pause the procedure and pass its continuation to a callback procedure. The callback may call the continuation at its convenience, resuming the original procedure.

Coroutines are particularly useful in conjunction with Agendas.

Scheme Procedure: call-with-coroutine thunk

Apply thunk within a coroutine prompt.

Scheme Syntax: coroutine body

Evaluate body within a coroutine prompt.

Scheme Syntax: colambda args body

Syntacic sugar for a lambda expression whose body is run within a coroutine prompt.

Scheme Syntax: codefine (name formals …) body

Syntacic sugar for defining a procedure called name with formal arguments formals whose body is run within a coroutine prompt.

Scheme Syntax: codefine* (name formals …) body

Syntacic sugar for defining a procedure called name with optional and keyword arguments formals whose body is run within a coroutine prompt.

Scheme Procedure: yield callback

Yield continuation to the procedure callback.