Does it have to be either/or?
FWIW, my view is that the correct interface for re-calling to a
co-routine with different arguments is that the previous C<yield>
should evaluate to the subsequent call's argument list.
In other words, when you jump back into a subroutine (just after the previous
C<yield>) that C<yield> appears to evaluate to the new argument list with
which you jumped back in:
*@args_of_next_call := yield $previous_yielded_value;
The point is, when implementing a particular co-routine, you can
choose to ignore the arg lists of each subsequent re-call:
sub next_fib (Int $a is copy, Int $b is copy) {
yield $a;
yield $b;
loop { ($a, $b) = ($b, $a+$b); yield $b }
}
or choose to update internal data using the subsequent arg lists:
sub next_fib (Int $a is copy, Int $b is copy) {
($a, $b) = yield $a;
($a, $b) = yield $b;
loop { ($a, $b) = ($b, $a+$b); ($a, $b) = yield $b }
}
And with a modest amount of extra effort, you can even implement the
"different-args-means-different-iterator" semantics:
sub next_fib (Int $a, Int $b) {
my sub fibber (Int $a is copy, Int $b is copy) is cached {
return {
yield $a;
yield $b;
loop { ($a, $b) = ($b, $a+$b); yield $b }
}
}
return fibber($a,$b).();
}
TMTOWTContinueI :-)
Damian
Thread Previous
|
Thread Next