Thread overview | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 17, 2004 A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Here's a minor suggestion: While D and many languages are 0-indexed with respect to arrays, a language I know of called Icon also has *negative subscripting*, namely, indexing backwards from the end of the array. Therefore, the last element could be given the index -1, the next to last -2, and so on. It's just a bit of synactic sugar, but I think a[-1] is slightly cleaner and neather than a[a.length-1]. Similarly, the index 0 could also stand for beyond the end of the array if used as the second value in a slice (but not the first), so a[0..0] is a slice of the entire array, a[1..0] is everything except the first element, and so on. I have to admit, however, that I think things would be neater if the second value in a slice was inclusive, thus making a[0..-1] a slice of the entire array, and a[0..0] just a[0]. I hope this might be of interest. James Fox |
February 17, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Fox | In article <c0u0pc$1tka$1@digitaldaemon.com>, James Fox says... > >Here's a minor suggestion: While D and many languages are 0-indexed with respect to arrays, a language I know of called Icon also has *negative subscripting*, namely, indexing backwards from the end of the array. Therefore, the last element could be given the index -1, the next to last -2, and so on. It's just a bit of synactic sugar, but I think a[-1] is slightly cleaner and neather than a[a.length-1]. Similarly, the index 0 could also stand for beyond the end of the array if used as the second value in a slice (but not the first), so a[0..0] is a slice of the entire array, a[1..0] is everything except the first element, and so on. > >I have to admit, however, that I think things would be neater if the second value in a slice was inclusive, thus making a[0..-1] a slice of the entire array, and a[0..0] just a[0]. > >I hope this might be of interest. > >James Fox > > I can't test it while at work, but I believe that's doable with this: char [] [int] foo; foo[-1] = "hi"; Sure, it's a hash table, but at least you have your negative indexes ;) Or, you could write a class with opIndex(int). ------------------- Carlos Santander B. |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Fox | Sorry, that's a bad idea. The reason is that we're all far too used to using -ve indexes in their "correct" mathematical context, i.e. a[-1] means get the element 1 before a[0] This comes about from the equivalence between subscripting syntax and pointer arithmetic. a[i] is equivalent to *(a + i). If i is -ve, then the arithmetic is no less valid. FYI, I once wrote a string class that committed this and many other crimes against good sense. It had a short and miserable life, and only remains as a reminder of how crap I once was. (This is actually featured as one of the little horrors in Appendix B in my new book "Imperfect C++".) "James Fox" <James_member@pathlink.com> wrote in message news:c0u0pc$1tka$1@digitaldaemon.com... > Here's a minor suggestion: While D and many languages are 0-indexed with respect > to arrays, a language I know of called Icon also has *negative subscripting*, > namely, indexing backwards from the end of the array. Therefore, the last element could be given the index -1, the next to last -2, and so on. It's just a > bit of synactic sugar, but I think a[-1] is slightly cleaner and neather than > a[a.length-1]. Similarly, the index 0 could also stand for beyond the end of the > array if used as the second value in a slice (but not the first), so a[0..0] is > a slice of the entire array, a[1..0] is everything except the first element, and > so on. > > I have to admit, however, that I think things would be neater if the second > value in a slice was inclusive, thus making a[0..-1] a slice of the entire array, and a[0..0] just a[0]. > > I hope this might be of interest. > > James Fox > > |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. |
> I can't test it while at work, but I believe that's doable with this:
> char [] [int] foo;
> foo[-1] = "hi";
> Sure, it's a hash table, but at least you have your negative indexes ;)
Er, but how does that make foo[-1] reference the same index as foo[foo.length-1]?
It's interesting, but I don't think it would work for D.
Sam
|
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | On Wed, 18 Feb 2004 11:44:13 +1100 (02/18/04 11:44:13) , Matthew <matthew.hat@stlsoft.dot.org> wrote: > Sorry, that's a bad idea. > > The reason is that we're all far too used to using -ve indexes in their > "correct" mathematical context, i.e. a[-1] means get the element 1 before > a[0] > > This comes about from the equivalence between subscripting syntax and > pointer arithmetic. a[i] is equivalent to *(a + i). If i is -ve, then the > arithmetic is no less valid. > > FYI, I once wrote a string class that committed this and many other crimes > against good sense. It had a short and miserable life, and only remains as a > reminder of how crap I once was. (This is actually featured as one of the > little horrors in Appendix B in my new book "Imperfect C++".) > I tend to agree with you on this Matthew. However the concept of a token that signifies a reference to the last element is still a useful idea. Off the top of my head, I suggest the Regular Expression symbol '$', such that a[$] refers to the last element. Thus things like a[4..$] and a[$-4..$-2], and a[$-var] would all be valid. -- Derek |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Fox | I think it's easy to make your own wrapper (template) class or struct using opIndex() or opSlice() bero |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Fox | While it was 2/17/04 9:23 PM throughout the UK, James Fox sprinkled little black dots on a white screen, and they fell thus: > Here's a minor suggestion: While D and many languages are 0-indexed with respect > to arrays, a language I know of called Icon also has *negative subscripting*, > namely, indexing backwards from the end of the array. Therefore, the last > element could be given the index -1, the next to last -2, and so on. It's just a > bit of synactic sugar, but I think a[-1] is slightly cleaner and neather than > a[a.length-1]. There would be a slight performance hit if we had to check the sign of every array subscript at run time, in the cases where it can't be determined at compile time. <snip> > I have to admit, however, that I think things would be neater if the second > value in a slice was inclusive, thus making a[0..-1] a slice of the entire > array, We already have this - a[]. > and a[0..0] just a[0]. A slice and an element aren't the same, even if it's a one-element slice. It's too late to change it now without breaking existing programs. And without reintroducing the fencepost errors that the semantics were presumably invented to cut out. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | >It's too late to change it now without breaking existing programs.
You realy did complete programs using D? You know that D still hasn't reached 1.0?
|
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | I'm all in favor of leaving the slice syntax as [beginix..lastix+1], and not in favor of adding the negative values as back from the end, BUT strongly in favor of having a $ or other shorthand for the lastix+1 ( = x.length ) value - the length is known, so the translation should be simple. I have already coded a painfully large number of those to clip off the front end of strings. The use of [0..0] gives an empty slice, as does "y = 0; z = 0; str[y..z]", but [4..4] causes an array bounds error. WHY? -------------------------------------------- In article <c0vgfa$18h9$1@digitaldaemon.com>, Stewart Gordon says... > >While it was 2/17/04 9:23 PM throughout the UK, James Fox sprinkled little black dots on a white screen, and they fell thus: > >> Here's a minor suggestion: While D and many languages are 0-indexed with respect to arrays, a language I know of called Icon also has *negative subscripting*, namely, indexing backwards from the end of the array. Therefore, the last element could be given the index -1, the next to last -2, and so on. It's just a bit of synactic sugar, but I think a[-1] is slightly cleaner and neather than a[a.length-1]. > >There would be a slight performance hit if we had to check the sign of every array subscript at run time, in the cases where it can't be determined at compile time. > ><snip> >> I have to admit, however, that I think things would be neater if the second value in a slice was inclusive, thus making a[0..-1] a slice of the entire array, > >We already have this - a[]. > >> and a[0..0] just a[0]. > >A slice and an element aren't the same, even if it's a one-element slice. > >It's too late to change it now without breaking existing programs. And without reintroducing the fencepost errors that the semantics were presumably invented to cut out. > >Stewart. > >-- >My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
February 18, 2004 Re: A Suggestion: Negative array subscripts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <opr3jv1uo3yj5swd@news.digitalmars.com>, Derek Parnell says... >I tend to agree with you on this Matthew. However the concept of a token that signifies a reference to the last element is still a useful idea. Off the top of my head, I suggest the Regular Expression symbol '$', such that a[$] refers to the last element. Thus things like a[4..$] and a[$-4..$-2], and a[$-var] would all be valid. Another programming language (lpc dialect) uses the < to symbolize indexing from the end: a[<1] == last entry a[<2..<1] == last two entries a[0..<1] == all entries And also, omitting the limits of a slice means the beginning or the end: a[1..] == all but the first a[..<2] == all but the last a[..2] == the first three |
Copyright © 1999-2021 by the D Language Foundation