Transfers control from the current coroutine back to the caller (when the
any2
tag is not given), or to some other coroutine
(specified by any2
) to continue execution at the point
where that coroutine had called yield
before. In the first
case, the value any
will be returned from the corresponding
co
call, in the second case it
will be the return value of that yield
call. If
prg
is given, it is executed in the destination environment
before the coroutine resumes execution. See also stack
, catch
and throw
.
: (co "rt1" # Start first routine
(msg (yield 1) " in rt1 from rt2") # Return '1', wait for value from "rt2"
7 ) # Then return '7'
-> 1
: (co "rt2" # Start second routine
(yield 2 "rt1") ) # Send '2' to "rt1"
2 in rt1 from rt2
-> 7
: (co 'a (let N 0 (loop (yield (inc 'N))))) # Incrementing coroutine
-> 1
: (co 'a T) # Next value
-> 2
: (yield NIL 'a (println N)) # Print value, then increment
2
-> 3
: (yield NIL 'a (println N))
3
-> 4
: (yield NIL 'a (yield N)) # Immediately yield back
-> 4 # Can be used to inspect a value
: (yield NIL 'a (yield N)) # in another coroutine
-> 4
: (co 'a T) # Next value
-> 5