Array indexing and slicing
Norbert Nemec
Norbert at Nemec-online.de
Mon Apr 3 12:02:43 PDT 2006
Luís Marques wrote:
> My experience of array slicing comes from Python, so I found it a bit surprising
> that a "array[2..]" notation didn't exist ("array[2:]" in Python). I later found
> the $ operator in others' code, which mostly fixes that.
The omissive indices are not yet supported, but they could be added as
an abbreviation without much trouble. It is just one character ("0" or
"$" vs. "") difference. I don't have a strong opinion on that point.
> How about supporting, as in Python:
>
> a[-1] == a[$];
That should be:
a[-1] == a[$-1]
> a[0..] == a[0..$];
>
> I personally dislike the $ operator, it seems a casual fix for a lack of better
> notation and someting you'd expect to find in Perl.
I actually quite like the notation. At first, I didn't like the idea of
"wasting" the precious ASCII character "$" for this profane purpose, but
compared to all other proposed solutions, this is the cleanest.
Also, to me "$" does not seem too counterintuitive. The character has
the long tradition of signifying the end of a line in regular
expressions, which somehow feels related to the issue at hand, even
though I have never seriously worked with Perl.
> I still prefer it to the more verbose "a[2 .. a.length]" notation, though.
That may be fine for "a", but it scales up very badly if you replace a
by a more complex expression.
The advantage of "$" will become even more obvious once there are is
better support for multidimensional arrays (yes, I'm still working on a
refined proposal for those) and the integer property "length" is
replaces by an integer array "shape":
consider adressing the interior (everything but the boundaries) of a 2D
array:
somearray[1..somearray.shape[0]-1,1..somearray.shape[1]-1]
vs.
somearray[1..$-1,1..$-1]
Or consider indexing via periodic boundary conditions on a 3D grid:
mygrid[x%mygrid.shape[0],y%mygrid.shape[1],z%mygrid.shape[2]]
vs.
mygrid[x%$,y%$,z%$]
> The verbose notation has the
> disadvantage of taking more whitespace; while most people read "a[2..3]" just
> fine it's a bit confusing to read "a[1..a.length]". Also, if you rename or use a
> different array you have two instances to correct, instead of one.
>
> The Python syntax seems to me a better generalization of the concepts. It
> doesn't require an extra operator, the indexes and the slice notation have all
> the expressiveness required.
>
> With omissive indexes (a[..7]) and wrapping indexes (a[-2]) the $ could go away.
These would be a bad replacement:
* omissive indexes are a nice abbreviation but have far less power than
the "$" symbol
* negative indices would have to be checked for at run time giving a
performace penalty that is unacceptable if D tries to aim for a position
in the league of high-performance computing languages.
More information about the Digitalmars-d
mailing list