News for hackers. Stuff that matters.
Re:Co-routines as an alternative to state machines by silentbicycle on Saturday August 29, 2009 @01:14PM (#793380)
An excellent paper on coroutines is "Revisiting Coroutines" by Ana Lucia de Moura and Roberto Ierusalimschy (http://www.inf.puc-rio.br/~roberto/docs/MCC15-04.pdf).

Lua has had excellent support for co-routines for a couple years, and, since its implementation is "stackless" (in the same sense as Stackless Python), its coroutines don't have the limitations as standard Python's. (It doesn't have a GIL either...) There are a couple papers about coroutines in Lua, but "Revisiting Coroutines" is the best overview. They're surprisingly versatile.

The paper shows how to efficiently implement one-shot continuations on asymmetric coroutines. (There is also a small C patch by Mike Pall that adds a coroutine.copy function to the VM, making multi-shot continuations possible: http://lua-users.org/lists/lua-l/2006-01/msg00652.html, and note the fix in his follow-up post.) Implementing continuations in terms of coroutines rather than the reverse (as is commonly done in Scheme) often leads to clearer code, IMHO.

There are no comments on this comment yet.