How does yieldAndReset work?

How does yieldAndReset work?
In this example I would have thought that the Routine would need to reset >before< calling play to return something other than nil.
However, this is not the case. It is good that it works - I would like to know why it works.

~a_part = Routine({

Number>>wait is simply Object>>yield, i.e.

// prints the clock time and x after 1 second
Routine({ 1.yield.postln; "x".postln }).play

Yielding anything other than a number at a scheduled routine stops it from continuing, i.e.

// doesn't print the clock time or continue to x
Routine({ "1".yield.postln; "x".postln }).play

Object>>yieldAndReset is the mechanism for restarting a routine from the inside.

// prints x every second, doesn't print clock time or continue to y
Routine({ "x".postln; 1.yieldAndReset.postln; "y".postln }).play

The routine you wrote is very subtle! To just print cats and dogs perhaps:

Routine({ "dog".postln; 1.wait; "cat".postln; 2.yieldAndReset }).play

Or perhaps:

{ { "dog".postln; 1.wait; "cat".postln; 2.wait }.loop }
1 Like