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

Reply via email to