[Grace-core] 0..(size-1) or 1..size

Michael Homer mwh at ecs.vuw.ac.nz
Tue Jul 5 18:10:40 PDT 2011


On Tue, Jul 5, 2011 at 9:00 PM, James Noble <kjx at ecs.vuw.ac.nz> wrote:
>> The answer (to me) is clearly 1 .. size.   That's how mathematicians index vectors and arrays, so it seems the natural default, and thus what one gets with a literal.
>
> Well I'd like that - let's see what Kim says when he gets back.
I can see how starting from 1 might be helpful for familiarity at the
earliest point, but most of the algorithms from only slightly later on
will be in terms of zero-based indices. Anything with modular
arithmetic will be unnecessarily complicated by correcting for the
offset every time, like a hash table. Having the indices form an open
interval [0,n) is also useful in those cases where the empty interval
can come up, like recursive searches and sorts. At the same time I
don't think there's much that a one-based index makes any simpler, and
as pointed out later in the message the occasions for actually using
the indices literally are pretty rare.

coll.eachWithIndex {} or for (coll) doWithIndex {}, and similar,
account for most uses, so writing out an explicit range or literal
index should be uncommon. It's just a matter of collections providing
the right set of methods. Most of the time indices should just be
opaque to the user, but for those times when it does matter what they
are it seems like zero is a better fit.
-Michael


More information about the Grace-core mailing list