On 3/7/07, Jonathan Lang wrote:
<summary snipped>
Looks good to me.
As well, the fact that @x[-1] doesn't refer to the element
immediately before @x[0] is awkward, as is the fact that @x[*-1]
doesn't refer to the element immediately before @x[*+0]. IMHO, it
would be cleaner to have @x[n] count forward and backward from the
front of the array, while @x[*+n] counts forward and backward from
just past the end of the array:
I suggested that at one point, so I'd agree that makes sense too. It
avoids the discontinuity at either end of the array -- although
arguably, points off the end of a list aren't in the same boat as
elements that actually exist, so the discontinuity might be
conceptually justified. (Make the weird things look weird?)
(In fact, the semantics for "@x[*+n]" follows directly from the fact
that an array returns the count of its elements in scalar context.)
And "@x[*]" would be the same as "@x[0..^*]" or "@x[0..(*-1)]".
That's an elegance in its favour.
One possible downside is that it wouldn't work for cyclic/wrap-around
arrays (where the indices are always interpreted mod n) -- since any
number would always refer to an existing element. Oh -- but if an
index isn't a plain counter, then it should be a named key, so scrap
that.
(The question then is: how to have "reducible" hash keys? By which I
mean different keys that get "reduced" to the same thing, e.g. %x{1}
=== %x{5} === %x{9} === %x{13}, etc. Presumably you can just
override the .{} method on your hash, right?)
You would lose one thing: the ability to select an open-ended Range
of elements. For a five-element list, "@x[1..^*]" means "@x[1, 2,
3, 4]", not "@x[1, 2, 3, 4, 5, 6, 7, 8, ...]".
Except wouldn't the .. interpret the * before the [] did? So 1..*
would yield a range-object from 1 to Inf, and then the array-deref
would interpret 1..Inf accordingly.
Actually, it seems more useful if the * could mean the count; you can
always say 1..Inf if that's what you want, but otherwise how would
you get [1..^*] meaning [1,2,3,4]? Perhaps the range could note when
it's occurring in []-context, and interpret the * as count rather
than as Inf?
-David