On Wed, Dec 03, 2008 at 02:26:57PM -0500, Mark J. Reed wrote:
> OK, so let's look at the general problem.  The structure is this:
> doSomething();
> while (someCondition())
> {
>     doSomethingElse();
>     doSomething();
> }
> ...and you want to factor out the doSomething() call so that it only
> has to be specified once.
> ...
> It does seem like a closure trait sort of thing, but I don't think
> it's currently provided by the p6 spec.

Perhaps PRE ... ?

    while (someCondition()) {
        PRE { doSomething(); }

Or, if you wanted to be sure that doSomething() is always called
at least once:

    repeat {
        PRE { doSomething(); }
    } while someCondition();

In the original post, this would result in:

    my $i;
    repeat {
        PRE { @stuff = grep { !.valid }, @stuff }
        .do_something( ++$i ) for @stuff;
    } while @stuff;

I don't know if the PRE block automatically throws an exception that 
needs to be caught, or if it simply prevents the block from being run.  
I'm guessing it throws an exception, but if so that should be catchable
w/o too much difficulty.


Reply via email to