Mason supports JavaScript's generator functions.
A generator function returns a generator object.
This has a next method that runs code up until the next yield and returns.
So, functions can look like normal code but actually be full of continuations.
For a JS-style overview, see here.
Generator functions should be marked with *\.
We've also seen . and ! prepended to \.
The order of function markers is:
.has newly boundthis*is a generator function!does not return any value
, all of them optional.
yield can get values from the context:
|| If these generator context functions are confusing, relax.
|| You'll probably never have to write one yourself.
taking = \@vals generator-func
generator = generator-func()
{value done} = generator.next()
cond done value for @vals
{value done} = generator.next _
if done
break value
taking 0..2 *\
. yield
. yield
. yield
It can also send values back to the context.
|| Injects itself into assignments and increments the number.
incrementing = \yields-numbers
generator = yields-numbers()
last-value = undefined
for
{value done} = generator.next last-value
if done
break value
last-value := + 1 value
incrementing *\
two = yield 1
three = yield two
yield three
yield* delegates to another generator.
yield-both = *!\a b yield a yield b new Stream *!\ yield* yield-both 1 2 yield* yield-both 3 4