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

Re: Maybe it's Just Nothing (was: Look-ahead arguments in for loops)

Thread Previous
From:
Jonathan Scott Duff
Date:
September 30, 2005 09:02
Subject:
Re: Maybe it's Just Nothing (was: Look-ahead arguments in for loops)
Message ID:
20050930160244.GA18661@pobox.com
On Thu, Sep 29, 2005 at 11:21:20PM -0600, Luke Palmer wrote:
[ discussion on undefs elided ]

Since we can annotate our undefs now, perhaps undefs that would be
generated because there are no previous or next elements get "tagged"
as such.  Something like:

        # assuming $b and $a are "before" and "after" elements
        for @list -> ?$b, $c, $?a {
            given $b {
                when undef but generated { say "a fake undef!"; }
                when undef               { say "a real undef!"; }
            }
        }

> Oh, right, and as for my favorite actual usage of for:
> 
>     for @list, :lookbehind(2) :lookahead(1)
>             -> $behind1, $behind2, $value, $ahead {
>         ...
>     }

Hmm.  Something like:

	for @list -> $c :behind($b1,$b2) :ahead($a1) { ... }

would seem to make a more direct connection between the variables and
what they are aliased to (if only there wasn't that use/mention problem
with the variables). 

I think there needs to be something that clearly and unambiguously says
that C<$c> is the value being iterated over and clearly makes a
correspondence between the other variables and their position relative
to C<$c> even with whatever other syntactic mumbling may be necessary.
(And maybe the proposed use of ? is it, but it feels wrong to me)

But, don't we have something like

        for @list.kv -> $i,$x { ...  }

and even if I'm misremembering @Larry's blessing on that particular
construct, we certainly have this:

        for zip(0..@list.elems(),@list) -> $i,$x { ... }

And then getting the values fore and aft of the current value is just a
matter of indexing into @list. This seems clearer to me than virtual
parameters that exist on either side of the sliding window of the "real"
parameters.

Also, since for seems to be some kind of non-consumptive iterator, maybe
we can get at it with some magical $?ITERATOR variable if we need to:

        for @list -> $x {
           my ($b1,$b2) = $?ITERATOR.prev(2);
           my ($a) = $?ITERATOR.next;                # defaults to next(1)
        }

Though that's far more syntax than using zip, but has the advantage
that it would work when @list really is a list rather than an array.

I still like using zip() or .kv and indexing the array directly. Putting
the values in an Array-like thingy seems to be a smallish price to pay
for easily getting at some number of elements before or after the
current element.

Rambling in a pre-caffienated way,

-Scott
-- 
Jonathan Scott Duff
duff@pobox.com

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