develooper Front page | perl.perl6.language | Postings from November 2003

Re: Control flow variables

Thread Previous | Thread Next
From:
Piers Cawley
Date:
November 19, 2003 01:35
Subject:
Re: Control flow variables
Message ID:
m24qx0iusq.fsf@obelisk.bofh.org.uk
Damian Conway <damian@conway.org> writes:

> David Wheeler wrote:
>
>> Isn't that just:
>>     for @array_of_random_values_and_types, 'ok' -> $t {
>>         when 'ok' { yada(); last }
>>         last unless some_sort_of_test($t);
>>     }
>> IOW, the topic is only 'ok' when all of the items in the array have
>> been processed
>
> Unless, of course, the string 'ok' is also one of the random_values_and_types.
> Hence my alternative solution.

Or:

   my $guard = Object.new;
   for @array_of_random_values_and_types, $guard 
     -> { when $guard { yada(); last }
          last unless some_sort_of_test($_) }

If there's some danger of some weird equality thing somehow causing a
problem there, you might need to make a Guard class with a very strict
'=~' implementation, but otherwise I can't see any problem.

Of course, that's better if some_sort_of_test is actually a method,
because then it becomes:

   my $guard = class { method some_sort_of_test { yada() }}.new;

   for *@array_of_random_values_and_types, $guard 
     -> { last unless .some_sort_of_test }

All of which means you can wrap it up in a macro and prove Simon's
point about what's syntax and what's CP6AN:

   macro unless_all( Block &test is parsed /<perl.expression>/,
                     Block &consequence, *@ary ) 
     { my $guard = Object.new;
       for *@ary, $guard 
         -> { when $guard { &consequence() ; last }
              when &test { last } } }

But I've probably got the signature slightly wrong. 

Higher Order Functions/Methods/Macros are great aren't they? 







Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About