On Tue, May 26, 2009 at 9:03 PM, Patrick R. Michaud <pmich...@pobox.com>wrote:

> On Tue, May 26, 2009 at 06:43:40PM -0500, John M. Dlugosz wrote:
> > Daniel Carrera daniel.carrera-at-theingots.org |Perl 6| wrote:
> >> The following construction doesn't do what a user might expect:
> >>
> >> for 0...@foo.elems -> $k { do_something($k,@foo[$k]) }
> >
> > Write ^...@foo.elems as a shortcut of 0...@foo.elems, which is the
> > variation to exclude that endpoint if you would rather not write
> > 0...@foo.elems-1.
>
> An even cleaner shortcut might be to use ^...@foo instead of ^...@foo.elems:
>
>    for ^...@foo -> $k { do_something($k, @foo[$k]) }
>
> Somewhat clearer could be:
>
>    for @foo.keys -> $k { do_something($k, @foo[$k]) }
>
> And some may prefer:
>
>    for @foo.kv -> $k, $v { do_something($k, $v) }
>
> I think the anti-pattern of "0...@foo.elems" (or its incorrect
> form "0...@foo.elems") should probably disappear in favor of
> the above forms instead.


Or perhaps

    for 0...@foo.end -> $k { ... }

@foo.keys may not be what the user wanted if @foo is a sparse array.

-Scott
-- 
Jonathan Scott Duff
perlpi...@gmail.com

Reply via email to