Hi all,

I occasionally find myself annoyed at having to do something like
this (I use Perl 5 vernacular, but it actually crops up in every
single language I have ever used):

    my $i;

    @stuff = grep !$_->valid, @stuff;

    while ( @stuff ) {
        $_->do_something( ++$i ) for @stuff;

        @stuff = grep !$_->valid, @stuff;
    }

Here, both the `while` condition and the `for` iteration assume
that [EMAIL PROTECTED] will contain only valid elements. Since I don’t
know whether this is initially the case, I have to repeat the
statement both before the loop and at its bottom.

There is no good way to rearrange this in the general case. The
only way to improve it at all is some variant on this:

    my $i;

    while (1) {
        @stuff = grep !$_->valid, @stuff;

        last if not @stuff;

        $_->do_something( ++$i ) for @stuff;
    }

Here I am forced to give up the formal loop conditional and bury
the termination condition somewhere in the middle of the loop
body. The code doesn’t exactly lie now, but it’s more coy about
its intent than necessary.

Does Perl 6 have some mechanism so I could write it along the
following obvious lines?

    my $i;
    while ( @stuff ) {
        $_->do_something( ++$i ) for @stuff;
    }

    # plus some way of attaching this fix-up just once
    { @stuff = grep !$_->valid, @stuff }

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Reply via email to