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

Re: Look-ahead arguments in for loops

Thread Previous
From:
Austin Hastings
Date:
September 29, 2005 11:08
Subject:
Re: Look-ahead arguments in for loops
Message ID:
433C2D9A.8070904@Yahoo.com
Dave Whipp wrote:

> 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
>   }
>
I like the idea. There's no reason the view window and the consumption
have to be the same.

=Austin

Thread Previous


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