Angel Faus writes:
: ¿are we going to have a iterator protocol? I am talking of python's 
: iterators, not ruby ones (which are being discussed in a different 
: post). The iterator protocol allows any object that implements a .next 
: method to be used in a foreach loop. This is very handy for things like 
: filehandles, recordsets, generators, coroutines and nearly any data 
: structure you can imagine.

Certainly.  That's how things like

    for 0 .. Inf -> $x { ... }

work.  We're certainly not going to generate all the values before
we start the loop...

: For example, filehandles can implent the iterator protocol out of the 
: box, so you can write:
: 
:  foreach $line in $fh {print $line}
: 
: Or maybe:
: 
:  foreach $line in $fh.lines {print $line}
: 
: Where $fh.lines is an object that implements the iterator protocol, not 
: an array of the lines (and thus much more efficient).

    for <$fh> -> $line { print $line }

: There was something about this in Apoc2, but I didn't get a clear 
: concolusion of it. The are a lot of useful applications of python-like 
: iterators, and perl6 is doing a great job of taking the coolest things 
: of other languages and integrating them into a choerent design, so i do 
: have a hope. [:)]

We'll schedule a clear conclusion to all this sometime after 6.0.0
comes out. :-)

: This is of course related to Ruby's iterators, that take a somehow 
: opposite solution to solve a similar problem.

Yes, though Perl 5's closures are just as powerful already.  What's needed
is some syntactic relief, however, so that it's easier to pass closures
other than as the first argument.  The

    -> $x { ... }

notation is the equivalent of Ruby's {|x| ... } notation.  But in Perl
you can pass the closure as any argument that wants a &block--it
doesn't have to come last.  Nor does it come in as a magical hidden
parameter.  And the equivalent of "yield" is simply &block().  (We'll
hold the yield keyword in reserve for returning a value without
returning control from a (thread, coroutine, iterator, generator,
continuation, whatever).

Larry

Reply via email to