develooper Front page | perl.perl6.language | Postings from July 2002

Re: Ruby iterators

Thread Previous | Thread Next
Michael G Schwern
July 2, 2002 18:02
Re: Ruby iterators
Message ID:
On Fri, Jun 28, 2002 at 01:21:50PM -0700, Erik Steven Harrison wrote:
> Over on Perlmonks someone was asking about Perl 6's ability to have named
> argument passing. He also asked about the Jensen Machine and Ruby iterators.
> Now, just being on this list has taught me so much, but, I'm not quite sure
> how it works, practically speaking, and whether or not we'll get in in P6.
> (I understand the abstract o fpass by name, but not how we use it). Could
> someone explain it to me, and tell me what the Perl 6 stance on the matter
> is?

* Yes, Perl 6 will have named arguments to subroutines.

What I can remember from the Perl 6 BoF is it will look something like this:

    sub foo ($this, $that) {
    	print $this if $that;

which is like:

    sub foo {
        my($this, $that) = @_;
	print $this if $that;

somebody else on this list can handle explaining how that all works better
than I can.  There's stuff about pointy subroutines, ->, method topics,
etc... *hand wave*

Even better, you can have small anonymous subroutines with implied

    @sorted_list = sort { $^a cmp $^b } @list;

which Perl will translate into the moral equivalent of:

    my $sub = sub ($^a, $^b) { $^a cmp $^b };
    @sorted_list = sort $sub, @list;

basically, it's a quick way to declare a subroutine while still having named
arguments for things like sort, map & grep.  Perl orders the arguments in
UTF8 order.

I *think* you will also be able to do this, at least I can't see why you
wouldn't be able to:

    @stuff = grep { length $^foo >= 42 } @list;

which is nice for nested greps and maps.  You don't have to fight over who
has $_.

So now grep/map/etc... will all take real code refs, so...

* Yes, Perl 6 will have the moral equivalent to Ruby iterators.

I'm particularly happy about the iterators, something that leapt out at me
when I learned Ruby as being very, very powerful.

For those who don't know what Ruby iterators are, you can go back through
the archives for the thread "Does this mean we get Ruby/CLU-style iterators?"
or an earlier thread:

From what I understand, the prototyping mechanism will be extended to allow
a method to take a block/code ref at the end of it's arguments.  Correct me
if I'm wrong, but while, for and such will all just be built-in subroutines.
So one can write something like this:

     class File;
     method foreach ($file, &block) {
     	    my $fh = open $file;
	    while( <$fh> ) {

     # open /usr/dict/words and print out each line.
     File.foreach '/usr/dict/words' {

The block is just an anonymous subroutine passed in as the second argument
to File.foreach().  In Perl 5, it would look something like this:

     package File;
     sub foreach {
         my($file, $block) = @_;
	 open(FILE, $file);
	 while(<FILE>) {
     File->foreach '/usr/dict/words', sub {

so what Perl 6 is really adding is the ability to write methods which look
like loops.

Ruby also has it's |$a| mechanism to name the arguments.  Presumably, you'll
get the same effect with the implied arguments I mentioned earlier.

    File.foreach '/usr/dict/words' {
        print $^line;

Something like that.

* What's a Jensen Machine?

This sig file temporarily out of order.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About