Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Control flow variables
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Luke Palmer  
View profile  
 More options Nov 18 2003, 9:48 am
Newsgroups: perl.perl6.language
From: fibon...@babylonia.flatirons.org (Luke Palmer)
Date: Tue, 18 Nov 2003 07:20:52 -0700
Local: Tues, Nov 18 2003 9:20 am
Subject: Control flow variables
I was reading the most recent article on perl.com, and a code segment
reminded me of something I see rather often in code that I don't like.
Here's the code, Perl6ized:

    ... ;
    my $is_ok = 1;
    for 0..6 -> $t {
        if abs(@new[$t] - @new[$t+1]) > 3 {
            $is_ok = 0;
            last;
        }
    }
    if $is_ok {
        push @moves: [$i, $j];
    }
    ...

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.

Python pulled this idiom out in to the syntax (yay them), with C<else>
on loops.  The else block on a python loop executes only if you never
broke out of the loop.  That's a great idea.

So, in Perl's postmodern tradition, I think we should steal that idea.
I'm a little uneasy about calling it C<else>, though.  Maybe C<FINISH>
would do, making the example:

    for 0..6 -> $t {
        if abs(@new[$t] - @new[$t+1]) > 3 {
            last;
        }
        FINISH {
            push @moves: [$i, $j];
        }
    }

I'd also like to say that while converting the rest of this sub to Perl
6, I realized how much I love the ..^ and ^.. operators.  I was
wondering whether people had forgotten about them :-).

Luke


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.