Chapter II ·
Idioms & deeper language
/
Lesson 14 of 27
Coroutines (preview)
You'll learn: Read the coroutine API and see why it's the engine behind stateful iterators.
Coroutines are cooperative threads inside a single OS thread:
create builds one, resume runs it, yield suspends it and
passes values back to the caller. They're the canonical engine
for stateful iterators and generator pipelines. The snippet
below is canonical Lua 5.3 — coroutines are on the Lua.ex
roadmap, so this lesson is read-only for now.
Read-only:
this snippet uses canonical Lua features that aren't implemented in Lua.ex yet. It's here as a reference.
Try it:
Re-read *Iteration*:
range(n) reached the same generator-shape result by capturing state in a closure instead of yielding from a coroutine.
See also
Iteration