John Macdonald wrote a lovely summary of coroutines [omitted]. Then added:
> I'd use "resume" instead of "coreturn"
We've generally said we'd be using "yield".
> and the interface for resume would allow values to be sent > in as well as out.
Indeed. As John suggested, the "yield" keyword (or whatever we call it) will evaluate to the argument list that is passed when the coroutine is resumed.
> sub byn(@base) is coroutine($count,@add) { > while(1) { > push(@base,@add); > return undef unless @base; > ($count, @add) = resume .call (splice @base, 0, $count ); > } > }
Under the "stupid" proposal, that would be:
sub byn(@base) { return coro ($count, [EMAIL PROTECTED] is copy) { # Anonymous coroutine while (1) { push @base, @add; return undef unless @base; ($count, @add) = yield splice @base, 0, $count; } } }
> my $co = byn(1..10); > > print $co.resume(3); # 1 2 3 > print $co.resume(2,50..56) # 4 5 > print $co.resume(10); # 6 7 8 9 10 50 51 52 53 54 > print $co.resume(10); # 55 56 > print $co.resume(10); # (undef) > print $co.resume(10); # exception or undef
Which would become:
my $co = byn(1..10);
print $co(3); # 1 2 3 print $co(2,50..56) # 4 5 print $co(10); # 6 7 8 9 10 50 51 52 53 54 print $co(10); # 55 56 print $co(10); # undef print $co(10); # exception
Damian