RFC 31 (v2) Subroutines: Co-routines

2000-09-18 Thread Perl6 RFC Librarian

This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Subroutines: Co-routines

=head1 VERSION

  Maintainer: Damian Conway [EMAIL PROTECTED]
  Date: 4 Aug 2000
  Last Modified: 18 Sep 2000
  Number: 31
  Version: 2
  Mailing List: [EMAIL PROTECTED]
  Status: Frozen

=head1 ABSTRACT

This RFC proposes the addition of a new function return command:
Cyield. Unlike Creturn, Cyield preserves the execution state of
the subroutine in which it's called, allowing the execution to be
resumed at the following statement, next time the subroutine is called.
It is also proposed that Cyields may nest, to simplify the
construction of recursive co-routines and iterators.

=head1 BACKGROUND

Normally, when a subroutine returns, its execution terminates
and it final context is completely lost. The next time the subroutine
is invoked, it recommences executing from its first statement.

In a coroutine, a value may be returned in such a way that the 
execution of the routine is Isuspended, along with all its 
local context. The next time the routine in invoked, its execution
resumes from the statement after the previous point of return.

=head1 DESCRIPTION

It is proposed to add a new control statement to Perl: Cyield.
A Cyield acts very much like a Creturn in that it terminates
execution of the enclosing subroutine and returns a value to its calling
context. However, when a value is Cyield'ed, the subroutine's execution
is suspended in such a way that it resumes from the following statement
next time the subroutine is invoked. 

Note that any subroutine containing a Cyield is implicitly a co-routine.
There is no explicit keyword or attribute proposed.

Coroutines make it very easy to implement generic parameteric closures
and iterators:

package Tree;

sub next_inorder ($self) {
yield $self-{left}-next_inorder if $self-{left};
yield $self;
yield $self-{right}-next_inorder if $self-{right};
return undef;
}


# and later...

while (my $node = $root-next_inorder()) {
print $node-{data};
}


Note that the above example implies (correctly) that yielding a result
which itself was yielded leaves the suspended execution of the
subroutine at the same Cyield statement (Inot the following
statement). Furthermore, yielding an Cundef is a no-op (i.e. it
doesn't cause the subroutine to return, but passes control to the next
statement).

Note that the arguments of a co-routine are ignored when it is resumed.
Hence:

sub every_second ( @vals ) {
yield (splice @vals, 0, 2)[0]  while @vals;
return;
}

Another interesting application is to make a Cmap block a coroutine,
to allow it to process hashes in a natural manner. For example, instead
of writing:

@newhash{map {transform_key($_)} keys %oldhash} =
map {transform_val($_)} values %oldhash;

one could write:

%newhash = map {yield transform_key($_); transform_val($_)} %oldhash;

This flattens the %oldhash to a sequence of key/value pairs. Then the
first time the map block is called (i.e. on a key) it transforms the
key and immediately yields it.  On the second iteration of the map, 
the block resumes after the Cyield and transforms the value.
That normal return resets the block so that for the next
iteration (another key) it applies the key transform and yields,
then tranforms the second value, etc., etc.


=head1 IMPLEMENTATION

Tricky.

=head1 REFERENCES

None.




Re: RFC 23 (v5) Higher order functions

2000-09-18 Thread Nathan Wiger

 =head2 Choice of notation
 
 The placeholder notation has been chosen to be consistent with the
 eisting Perl scalar notation (but using a ^ prefix rather than a $):
 
 RoleScalar  Placeholder
  var  analog
 
 named   $x, $y^x, ^y
 positional$0, $1, $2^0, ^1, ^2
 default   $_^_

As I voiced before, this is incorrect. $0 is not a positional scalar. $1
is the first positional scalar.

Either we need to change $1 to $0, or change ^0 to ^1. Considering $0
has been around a little while longer than HOFN, I strongly suggest we
change ^0 to ^1 to be consistent.

I realize this RFC has been frozen, but this is an important issue. And
remember, Mike Pastore and I were the ones who suggested this whole ^
notation, y'know. ;-)

Other than this issue the RFC is excellent^Wamazing.

-Nate