> Date: Mon, 18 Nov 2002 09:28:59 +1100
> From: Damian Conway <[EMAIL PROTECTED]>

I've a couple of questions here:

> we still have implicit iteration:
> 
>      for fibs() {
>          print "Now $_ rabbits\n";
>      }

Really?  What if fibs() is a coroutine that returns lists (Fibonacci
lists, no less), and you just want to iterate over one of them?  The
syntax:

     for &fibs {
         print "Now $_ rabbits\n";
     }

Would make more sense to me for implicit iteration.  Perhaps I'm not
looking at it right.  How could you get the semantics of iterating
over just one list of the coroutine?

> and explicit iteration:
> 
>      my $iter = fibs();
>      while <$iter> {
>          print "Now $_ rabbits\n";
>      }

Ahh, so $iter is basically a structure that has a continuation and a
value.  When you call the .next method, it calls the continuation, and
delegates to the value otherwise.  Slick  (Unless the coroutine itself
is returning iterators... then... what?).

    class Foo {
        method next { print "Gliddy glub gloopy\n" }
    }
    sub goof () {
        loop {
            print "Nibby nabby nooby\n";
            yield new Foo;
        }
    }
    
    my $iter = goof;
    print $iter.next;  # No.. no!  Gliddy!  Not Nibby!

How does this work, then?

> For example, instead of the semantics I proposed previously:
> 
>      # Old proposal...
> 
>      sub pick_no_repeats (*@from_list) {
>          my $seen;
>          while (pop @from_list) {
>              next when $seen;
>              @from_list := yield $_;
>              $seen |= $_;
>          }
>      }

Hang on... is C<while> a topicalizer now?  Otherwise this code is not
making sense to me.

> These semantics also rather neatly solve the problem of whether or
> not to re-evaluate/re-bind the parameters each time a coroutine
> is resumed. The rule becomes simple: if the iterator's C<next>
> method is invoked without arguments, use the old parameters;
> if it's invoked with arguments, rebind the parameters.
> 
> And the use of the <$foo> operator to mean $foo.next cleans up
> teh syntax nicely.

So filehandles are just loops that read lines constantly and yield
them.  I'm really starting to like teh concept of tohse co-routines :)
They elegantify stuff.

Luke

Reply via email to