Thread overview | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 06, 2004 Negative array indices? | ||||
---|---|---|---|---|
| ||||
Hi there, I wonder whether this has been discussed before: * How about allowing negative array indices to count backward from the end of the array? * How about allowing to drop one of the bounds of a range to indicate beginning or end of the array? An example should make clear what I mean: char[] str = "0123456789" assert(str[2] == '2'); assert(str[-3] == '7'); assert(str[1..3] == "12"); assert(str[..4] == "0123"); // just for completeness // could be written as str[0..4] just as well str[-11]; // throws ArrayBoundsError assert(str[7..] == "789"); assert(str[-4..] == "6789"); assert(str[4..-2] == "4567"); One question I could not resolve for myself: should illegal ranges throw an ArrayBoundsError or return an empty/truncated string? One way would be an extremely tolerant behaviour like: assert(str[4..2] == ""); assert(str[-2..3] == ""); assert(str[7..22] == "789"); assert(str[-7..8] == "34567"); Alternatively, one could argue that each case should throw an ArrayBoundsError. What is the current behaviour? Ciao, Nobbi |
May 06, 2004 Re: Negative array indices? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | "Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:c7co59$11i4$1@digitaldaemon.com... > Hi there, > > I wonder whether this has been discussed before: > > * How about allowing negative array indices to count backward from the end of the array? That stinks > * How about allowing to drop one of the bounds of a range to indicate beginning or end of the array? That doesn't. |
May 06, 2004 Re: Negative array indices? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | Norbert Nemec wrote: >Hi there, > >I wonder whether this has been discussed before: > >* How about allowing negative array indices to count backward from the end >of the array? > > Yes it has. -- -Anderson: http://badmama.com.au/~anderson/ |
May 06, 2004 Re: Negative array indices? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | To be less facetious. The reason it's a very bad idea is that array subscripting in C and C++ and D can be done with signed integers because it is legal _and meaningful_ to pass a -ve subscript to mean prior to the given base (pointer and/or array). Since D's support of C constructs most certainly encompasses this very important, albeit dangerous, construct, it would be nonsensical to have built-in D arrays use a back-relative offset and pointers use -ve offset. It would be a total killer. "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c7d5bc$1lsl$1@digitaldaemon.com... > Norbert Nemec wrote: > > >Hi there, > > > >I wonder whether this has been discussed before: > > > >* How about allowing negative array indices to count backward from the end of the array? > > > > > Yes it has. > > -- > -Anderson: http://badmama.com.au/~anderson/ |
May 06, 2004 Alternative syntax (was: Negative array indices?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
> To be less facetious. The reason it's a very bad idea is that array subscripting in C and C++ and D can be done with signed integers because it is legal _and meaningful_ to pass a -ve subscript to mean prior to the given base (pointer and/or array).
>
> Since D's support of C constructs most certainly encompasses this very important, albeit dangerous, construct, it would be nonsensical to have built-in D arrays use a back-relative offset and pointers use -ve offset. It would be a total killer.
OK, I understand this. How about finding another syntax for describing a range bound that is counted backwards from the end of an array? p.e.
char[] str = "0123456789"
assert(str[<3] == '7');
assert(str[<4..] == "6789");
assert(str[4..<2] == "4567");
The < as a prefix does not exist yet and would only be valid for indices or ragne bounds, just like the .. infix operator. Alternative syntax proposals are probably easy to find.
In general, some way to indicate this "counting back from the end" really improves string handling qualities of a language.
Ciao,
Nobbi
|
May 06, 2004 Re: Alternative syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | Norbert Nemec wrote: >Matthew wrote: > > > >>To be less facetious. The reason it's a very bad idea is that array >>subscripting in C and C++ and D can be done with signed integers because >>it is legal _and meaningful_ to pass a -ve subscript to mean prior to the >>given base (pointer and/or array). >> >>Since D's support of C constructs most certainly encompasses this very >>important, albeit dangerous, construct, it would be nonsensical to have >>built-in D arrays use a back-relative offset and pointers use -ve offset. >>It would be a total killer. >> >> > >OK, I understand this. How about finding another syntax for describing a >range bound that is counted backwards from the end of an array? p.e. > > char[] str = "0123456789" > assert(str[<3] == '7'); > assert(str[<4..] == "6789"); > assert(str[4..<2] == "4567"); > >The < as a prefix does not exist yet and would only be valid for indices or >ragne bounds, just like the .. infix operator. Alternative syntax proposals >are probably easy to find. > >In general, some way to indicate this "counting back from the end" really >improves string handling qualities of a language. > >Ciao, >Nobbi > > This has been discussed before, a few times. It's only syntax sugar but a good idea. -- -Anderson: http://badmama.com.au/~anderson/ |
May 06, 2004 Re: Alternative syntax (was: Negative array indices?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec |
>OK, I understand this. How about finding another syntax for describing a range bound that is counted backwards from the end of an array? p.e.
>
> char[] str = "0123456789"
> assert(str[<3] == '7');
> assert(str[<4..] == "6789");
> assert(str[4..<2] == "4567");
>
How about defining a symbol for the str.length such as '$', then
char[] str = "0123456789"
assert(str[$-3] == '7');
assert(str[$-4..$] == "6789");
assert(str[4..$-2] == "4567");
and since '$' already has the meaning "end of line" in other contexts readability is maintained without the clutter of 'str.length'.
Mark.
|
May 06, 2004 Re: Alternative syntax (was: Negative array indices?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | "Mark" <Mark_member@pathlink.com> wrote in message news:c7dmj7$2lsu$1@digitaldaemon.com... > > >OK, I understand this. How about finding another syntax for describing a range bound that is counted backwards from the end of an array? p.e. > > > > char[] str = "0123456789" > > assert(str[<3] == '7'); > > assert(str[<4..] == "6789"); > > assert(str[4..<2] == "4567"); > > > > > How about defining a symbol for the str.length such as '$', then > > char[] str = "0123456789" > assert(str[$-3] == '7'); > assert(str[$-4..$] == "6789"); > assert(str[4..$-2] == "4567"); > > and since '$' already has the meaning "end of line" in other contexts readability is maintained without the clutter of 'str.length'. Weird city. I was thinking just the same thing, but without the erudite rationale. Consider yourself "hear, hear"'d! |
May 06, 2004 Re: Alternative syntax (was: Negative array indices?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | Mark wrote: > How about defining a symbol for the str.length such as '$', then > > char[] str = "0123456789" > assert(str[$-3] == '7'); > assert(str[$-4..$] == "6789"); > assert(str[4..$-2] == "4567"); > > and since '$' already has the meaning "end of line" in other contexts readability is maintained without the clutter of 'str.length'. I like that one! It would not only be more readable but also more flexible than my idea. Consider for example things like: str[..$/2] It would make even more sense for multidimensional arrays, since there str.length would become str.range[i] with i indexing the different dimensions. Just consider: mymatrix[..$-2,..$-2] as syntactic sugar for mymatrix[0..mymatrix.range[0]-2,0..mymatrix.range[1]-2] |
May 07, 2004 Re: Alternative syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On Thu, 6 May 2004 15:45:43 +0000 (UTC), Mark wrote: >>OK, I understand this. How about finding another syntax for describing a range bound that is counted backwards from the end of an array? p.e. >> >> char[] str = "0123456789" >> assert(str[<3] == '7'); >> assert(str[<4..] == "6789"); >> assert(str[4..<2] == "4567"); >> > > > How about defining a symbol for the str.length such as '$', then > > char[] str = "0123456789" > assert(str[$-3] == '7'); > assert(str[$-4..$] == "6789"); > assert(str[4..$-2] == "4567"); > > and since '$' already has the meaning "end of line" in other contexts readability is maintained without the clutter of 'str.length'. Agreed. I have been advocating this exact same thing for the Euphoria language for ages now. -- Derek Melbourne, Australia |
Copyright © 1999-2021 by the D Language Foundation