develooper Front page | perl.perl6.language | Postings from September 2005

Look-ahead arguments in for loops

Thread Next
From:
Dave Whipp
Date:
September 29, 2005 09:41
Subject:
Look-ahead arguments in for loops
Message ID:
20050929164142.2665.qmail@lists.develooper.com
Imagine you're writing an implementation of the unix "uniq" function:

    my $prev;
    for grep {defined} @in -> $x {
        print $x unless defined $prev && $x eq $prev;
        $prev = $x;
    }

This feels clumsy. $prev seems to get in the way of what I'm trying to 
say. Could we imbue optional binding with the semantics of not being 
consumed?

   for grep {defined} @in -> $item, ?$next {
     print $item unless defined $next && $item eq $next;
   }

The same behavior, but without the variable outside the loop scope.


It would also be good not to overload the meaning of $?next to also tell 
us if we're at the end of the loop. In addition to FIRST{} and LAST{} 
blocks, could we have some implicit lexicals:

   for @in -> $item, ?$next {
     print $item if $?LAST || $item ne $next
   }

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