Since a FIRST block gets called at loop initialization time, it seems to me
that it would be useful to have a block closure trait, RESUME, that gets
called at the beginning of every loop iteration except the first. Thus, at
the beginning of each loop iteration either FIRST or RESUME but not both
would get called. Other possible names for this block include REENTER,
SUBSEQUENT, or NOTFIRST. 

RESUME would have many uses.  For example, to put commas in the right places
in a printed list:

for @list -> @value {
    print $value;
    RESUME {print ', ';} #If this were NEXT we would print an extra comma at
the end.
    LAST {print "\n";}
}

RESUME could also be useful for maintaining state information, such as loop
iteration or running sum, between loop iterations:

for @data -> $value {
   state $index will first {$_ = 0;} will resume {++$_;}
   state $sum will first {$_ = $value;} will resume {$_ += value;}
   ...
}


Joe Gottman 




Reply via email to