Coroutines/Generators

I made a project that implements Lua coroutines or generators using first-class continuations. Continuations are cool.

Snap! coroutines (berkeley.edu)

I made a fibonacci sequence example. Press the green flag, and then press space to show the next fibonacci number.

What's a coroutine?

A coroutine is basically a function that can be paused during execution (yielded) and resumed at a later time. When a coroutine is called, it runs the coroutine until it yields. When the same coroutine instance is called again, it resumes the coroutine at the last place it yielded. In my implementation, when a coroutine yields, it can also pass information to the caller, as the return value to the function to call the coroutine.

Insanely creative :wink:
But it seems that call stack will be leaked and because of indirect circular reference no optimisation (eg tail call elimination) can be applied.

I think this coroutine library would be very good for stopping audio and continuing it.
Does the library work with audio?

No, it doesn't pause sounds. In Snap!, running a different continuation doesn't pause sounds that it's playing.

I'm sorry but I'm not sure what you mean by the call stack leaking, and what causes the indirect circular reference.

Would rearranging the script like this fix the problems you mentioned?
image

I was thinking of the sequence: cmd continuation calls the tag continuation (yield), then the cmd cont. is called again...

Oh, it's not that important with finite generator/coroutine.

And Python's implementation.

("Pzthonäs"? QWERTZ is a bit hard to get used to.)