Generators Library

Hi guys! I made a Generators library!, loosely like JavaScript generators.


How it works:
untitled script pic (42)
Creates a new generator. GENERATOR representing it.
untitled script pic (43)
Yields the generator. The second value is captured by NEXT VALUE.
untitled script pic (44)
Finishes a generator function.


untitled script pic (45)
Reports the next value from a generator, given by YIELD.
untitled script pic (47)
Reports if the generator is done.


Example:


I’m afraid you’ve missed a point with generators in JS.
Generators are not coroutines, just an iterator variant, and

With a generator function, values are not evaluated until they are needed

:link: (Generator - JavaScript | MDN) :link:
The generator body should not be running between calls to the next value.

You may look for information about Generators script pic (1) . They can be used to suspend/resume parts of your program.

As for Your code


Counter item(2) probably should be set at yield but tested at next value.
Or maybe it should be used as a semaphore to synchronize yield/next as they must work in strict cooperation - exactly one next for every yield.

You may also look at the Streams library.