Thread overview | |||||
---|---|---|---|---|---|
|
March 22, 2006 Negative index for arrays | ||||
---|---|---|---|---|
| ||||
I see that this is used in Python's strings, to get a char from the end of the string. Would there be any drawback in implementing the same thing in D's arrays ? Something like : array[-x] = array[array.length - x] |
March 22, 2006 Re: Negative index for arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Muta | Muta skrev:
> I see that this is used in Python's strings, to get a char from the end of
> the string. Would there be any drawback in implementing the same thing in
> D's arrays ? Something like :
> array[-x] = array[array.length - x]
The drawback I can see is that it needs a runtime check every time an array is indexed. I would classify that as a stopper. Instead, in D, you can write:
array[$-1]
/Oskar
|
March 22, 2006 Re: Negative index for arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oskar Linde | In article <dvrapu$cpd$1@digitaldaemon.com>, Oskar Linde says... > >Muta skrev: >> I see that this is used in Python's strings, to get a char from the end of >> the string. Would there be any drawback in implementing the same thing in >> D's arrays ? Something like : >> array[-x] = array[array.length - x] > >The drawback I can see is that it needs a runtime check every time an array is indexed. I would classify that as a stopper. Instead, in D, you can write: > >array[$-1] > >/Oskar You can always fake it. In the following code i can be either positive or negative: array[(i+$)%$] Jacques |
Copyright © 1999-2021 by the D Language Foundation