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.
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