I don't see how you could interpret the name "elems" as something
returning "index of the last element".  If your IRC interlocutor
confused @foo.elems with $#foo, then it seems more likely that they
were confused about the semantics of $#foo than of .elems , whose p5
equivalent is just scalar(@foo).
  Of course, arrays itemify to references in p6, but they still numify
to their length, so +...@foo is a synonym for @foo.elems, and I suspect
it will be the more common form. Namewise, @foo.elems isn't obviously
distinct enough from @foo.values, IMO, whereas numeric context is
unambiguous.
   Anyway, this feels like the common confusion of natural numbers vs
offsets.  P6 mitigates that confusion somewhat by providing ^N as a
shortcut for the commonly-needed 0..N-1 range, and I don't think that
idiom should be eschewed by the doc.  Yes, @foo.keys and @foo.kv are
better in the particular case of looping over a single array's
indices, but ^ and .elems can be useful together, too. Consider the
case of processing unequal-length arrays in parallel:

for ^max(@foo.elems,@bar.elems)



On 5/27/09, Daniel Carrera <daniel.carr...@theingots.org> wrote:
> Patrick R. Michaud wrote:
>> 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.
>
> Even if there is no language change, at least it'd be good to ensure
> that "0...@foo.elems" doesn't appear in the documentation. Instead,
> whoever writes the docs should use @foo.keys and @foo.kv. Those are
> *very* clear, and they do the right thing.
>
> Daniel.
>

-- 
Sent from my mobile device

Mark J. Reed <markjr...@gmail.com>

Reply via email to