On 10/29/2013 03:19 PM, MRAB wrote:
On 29/10/2013 17:49, Ron Adam wrote:
On 10/28/2013 10:43 PM, MRAB wrote:
I think a reverse index object could be easier to understand. For now it could be just a subclass of int. Then 0 and rx(0) would be distinguishable from each other. (-i and rx(i) would be too.)
seq[0:rx(0)] Default slice. seq[0:rx(0):-1] Reversed slice. (compare to above)
seq[rx(5): rx(0)] The last 5 items.
A syntax could be added later. (Insert preferred syntax below.)
seq[\5:\0] The last 5 items
If you're going to have a reverse index object, shouldn't you also have an index object?
I don't like the idea of counting from one end with one type and from the other end with another type.
It would be possible to make it work both ways by having a direction attribute on it which is set with a unary minus opperation.
seq[-ix(5): -ix(0)]
Positive integers would work normally too. Negative ints would just be to the left of the first item rather than the left of the last item.
Just had a thought. In accounting negative numbers are often represented as a positive number in parenthes.
seq[(5,):(0,)] Last 5 items.
Unfortunately we need the comma to define a single item tuple. :-/
But this wuold work without adding new syntax or a new type. And the ',' isn't that big of a deal. It would just take a bit of getting used to it.
But if you're really set on having different types of some kind, how about real counting from the left and imaginary counting from the right:
seq[5j : 0j] # The last 5 items
seq[1 : 1j] # From second to second-from-last
[snip] Suppose there were two new classes, "index" and "rindex". "index" counts from the left and "rindex" counts from the right.
You could also use unary ">" and "<":
>x == index(x) <x == rindex(x)
Slicing would be like this:
seq[<5 : <0] # The last five items seq[>1 : <1] # From the second to the second-from-last.
Strictly speaking, str.find and str.index should also return an index instance. In the case of str.find, if the string wasn't found it would return >-1 (i.e. index(-1)), which, when used as an index, would raise an IndexError (index(-1) isn't the same as -1).
In fact, index or rindex instances could end up spreading throughout the language, to wherever an int is actually an index. (You'd also have to handle addition and subtraction with indexes, e.g. pos + 1.)
All of which, I suspect, is taking it too far! :-)
I think it may be the only way to get a clean model of slicing from both directions with a 0 based index system. Cheers, Ron