[OT] Should exist / does exist?

Andy Armstrong andy at hexten.net
Thu Dec 7 10:20:31 GMT 2006


On 7 Dec 2006, at 09:34, Adriano Ferreira wrote:
> That made me curious: what is the Ruby expression to do that? IMO it
> looks like Ruby must have the same unsolved issue. Or am I wrong?

No, you're precisely right - Ruby seems to have the same problem. I  
had a further play last night. As I mentioned I'm leaning towards a  
mixin module that adds redo|next|last to a module like this:

package MyIter
use BlockLoopMagicThing;

sub forThings {
    my $self = shift;
    my $cb   = shift;

    eval {
       for my $obj (@{$self->{objs}}) { # or whatever
          $self->yield_to($cb, $obj);
       }
    };
    if ($self->is_last($@)) {
        # $iter->last called
    } else {
        die $@;
    }
}

... later ...

$iter->forThings(sub {
     my $thing = shift;
     if ($thing->nasty) {
         $iter->next;
     }
     if ($thing->can_be_fixed) {
         $iter->redo;
     }
     if ($thing->is_the_one_we_want) {
         $iter->last;
     }
});

The mixin would provide redo, next, last, yield_to, is_last and maybe  
others. redo and next can be handled by yield_to - next is just an  
early exit from a single iteration and redo just calls the block  
again with the same parameters. Often the only thing that needs to be  
communicated back to the iterator is last - because how you get out  
of the iterator depends on its implementation.

-- 
Andy Armstrong, hexten.net



More information about the london.pm mailing list