> -----Original Message-----
> From: Damian Conway [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, November 18, 2003 4:02 PM
> To: Language List
> Subject: Re: Control flow variables
>
>
> Luke Palmer started a discussion:
>
>
> > I see this idiom a lot in code.  You loop through some values on a
> > condition, and do something only if the condition was never true.
> > $is_ok is a control flow variable, something I like to minimize.  Now,
> > there are other ways to do this:
> >
> >     if (0..6 ==> grep -> $t { abs(@new[$t] - @new[$t+1]) })
> >     { ... }
> >
> > But one would say that's not the cleanest thing in the world.
>
> Only because you overdid the sugar. :
>
>      if grep {abs(@new[$^t] - @new[$^t+1]) > 3} 0..6
>      { ... }
>
> is pretty clean.
>
> But, in any case, handling exceptional cases are what exceptions are for:
>
>      try {
>          for 0..6 -> $t {
>              die if abs(@new[$t] - @new[$t+1]) > 3;
>          }
>       CATCH {
>              push @moves: [$i, $j];
>          }
>      }
>
> As regards return values of control structures, I had always assumed that:
>
>      * scalar control structures like C<if>, C<unless>, and C<given>
>        return the value of the last statement in their block that they
>        evaluated;
>
>      * vector control structures like C<loop>, C<while>, and C<for> in a
list
>        context return a list of the values of the last statement each
>        iteration evaluated;
>
>      * vector control structures like C<loop>, C<while>, and C<for> in
>        a scalar context return an integer indicating the number of times
>        their block was iterated.

This seems excessive, but easily discarded during optimization. On the other
hand, I don't trust the "last statement evaluated" behavior for loops, since
the optimizer could very well do surprising things to loop statements.
(Likewise, however, for scalar control structures.)

What does this say about order of evaluation?

  if @arry.length != while @arry { transmit(@arry.pop) or last; }
  {
    print "Couldn't send them all.";
  }

I'm pretty sure that has to have unspecified behavior, but ... comment?

>
> So we might also write:
>
>      for 0..6 -> $t {
>         last if abs(@new[$t] - @new[$t+1]) > 3;
>      }
>      < 7 and push @moves: [$i, $j];
>
>
> Damian

Oh goody. Another (ab)use for named blocks:

  my &named_block = for 0..6 -> $t { last if abs (@new[$t] - @new[$t+1]) >
3; }

  push @moves: [$i, $j] if &named_block() < 7;

This seems hideously powerful, but at the same time dangerous. Kind of like
select.

=Austin

Reply via email to