November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | The point is that we need to know if array slice indices are both inclusive, or if the 'end' index is exclusive. Do you write: a[0 .. a.length] // exclusive or a[0 .. a.length-1] // inclusive ? The second makes a lot more intuitive sense. For a 1-element array thus: int a[1]; would you want people doing this:? int b[1] = a[0 .. 1]? or this: int b[1] = a[0 .. 0]? Sean "Walter" <walter@digitalmars.com> wrote in message news:9td77n$4fc$1@digitaldaemon.com... > > "Pavel Minayev" <evilone@omen.ru> wrote in message news:9tcv3f$2vca$1@digitaldaemon.com... > > "Walter" <walter@digitalmars.com> wrote in message news:9tche0$2n2v$3@digitaldaemon.com... > > > > > But the length is not the maximum index. > > > > Then [0..length] is not legal. > > I don't understand what you're driving at, then. |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Ok, the first index is inclusive, the second is exclusive. Making them both inclusive would make it impossible to write a 0 length array. -Walter "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tdarf$6mq$1@digitaldaemon.com... > The point is that we need to know if array slice indices are both inclusive, > or if the 'end' index is exclusive. > > Do you write: > > a[0 .. a.length] // exclusive > > or > > a[0 .. a.length-1] // inclusive > > ? > > The second makes a lot more intuitive sense. For a 1-element array thus: > > int a[1]; > > would you want people doing this:? > > int b[1] = a[0 .. 1]? > > or this: > > int b[1] = a[0 .. 0]? > > Sean > > > "Walter" <walter@digitalmars.com> wrote in message news:9td77n$4fc$1@digitaldaemon.com... > > > > "Pavel Minayev" <evilone@omen.ru> wrote in message news:9tcv3f$2vca$1@digitaldaemon.com... > > > "Walter" <walter@digitalmars.com> wrote in message news:9tche0$2n2v$3@digitaldaemon.com... > > > > > > > But the length is not the maximum index. > > > > > > Then [0..length] is not legal. > > > > I don't understand what you're driving at, then. > > > |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | But we're talking about slicing....if you want a 0 length array, then just declare one! int[0] a; This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org... > But we're talking about slicing....if you want a 0 length array, then just declare one! > > int[0] a; > > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. Agreed! And if you need zero-length array, just use the following form: int a[]; a[x..x-1]; // no elements |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | > int a[];
> a[x..x-1]; // no elements
In fact, I think that the form a[x..y] where x is
greater than y should be considered an "empty slice".
|
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org... > But we're talking about slicing....if you want a 0 length array, then just > declare one! > int[0] a; A 0 length array should be a valid slice. > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. I see it from a different angle. I see arrays in C as being 0 based, inclusive, and the number of elements is specified, exclusive. The slice semantics fit right in with that. |
November 21, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter |
Walter wrote:
>
> "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org...
> > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive.
>
> I see it from a different angle. I see arrays in C as being 0 based, inclusive, and the number of elements is specified, exclusive. The slice semantics fit right in with that.
Well, sort of. Your slice notation preserves the "one-past-last-index" semantic of C array counts, but it loses the "count of elements" semantic, which I feel is the more important one. If your slice notation used counts instead of off-the-end indices, I'd have more support for it.
Hmm. How about [count@offset]:
int original[10]; // ten elements
int slice[] = original[4@3]; // elements [3][4][5][6]
You'd read that as "four [elements, starting] at three".
Thus "normal" array notation [10] would be equivalent to [10@0], "ten elements starting at zero".
There's some horrifying concept teasing the back of my brain about using this slice notation in a declaration to get N-based instead of zero-based arrays:
int PascalRules[10@1]; // one-based array
PascalRules[ 1 ] = 77; // initial element
PascalRules[ 10 ] = 44; // final element
PascalRules[ 0 ] = 99; // array bounds exception
...but that's probably just asking for trouble.
-RB
|
November 21, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Borogove | "Russell Borogove" <kaleja@estarcion.com> wrote in message news:3BFAF429.826B32D5@estarcion.com... > Walter wrote: > > > > "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org... > > > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. > > > > I see it from a different angle. I see arrays in C as being 0 based, inclusive, and the number of elements is specified, exclusive. The slice semantics fit right in with that. > > Well, sort of. Your slice notation preserves the "one-past-last-index" semantic of C array counts, but it loses the "count of elements" semantic, which I feel is the more important one. If your slice notation used counts instead of off-the-end indices, I'd have more support for it. I've tried some real code with it, and it works out well. All I can ask is give it a chance! > Hmm. How about [count@offset]: > > int original[10]; // ten elements > int slice[] = original[4@3]; // elements [3][4][5][6] > > You'd read that as "four [elements, starting] at three". > > Thus "normal" array notation [10] would be equivalent to [10@0], "ten elements starting at zero". > > There's some horrifying concept teasing the back of my brain about using this slice notation in a declaration to get N-based instead of zero-based arrays: > > int PascalRules[10@1]; // one-based array > > PascalRules[ 1 ] = 77; // initial element > PascalRules[ 10 ] = 44; // final element > PascalRules[ 0 ] = 99; // array bounds exception > > ...but that's probably just asking for trouble. I've been trying to avoid using '@' <g>. |
November 21, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | I don't see that as a critical constraint...readability seems far more important. However, you mention programs where this syntax makes things easy...I would really like to see some examples. It might help me understand what you're coming from. BTW, I think that if we're not going to use inclusive ranges on BOTH sides, then R. Borogoves idea ( a = b[3@4] ) or something like it would be far superior. First/Last - intuitive Start/Len - intuitive First/Last+1 - might be easy to type, but not intuitive :( Walter wrote: > "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org... > > But we're talking about slicing....if you want a 0 length array, then just > > declare one! > > int[0] a; > > A 0 length array should be a valid slice. > > > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
November 22, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | Check out: www.digitalmars.com/d/ctod.html "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFBCC60.2EB7049C@deming-os.org... > I don't see that as a critical constraint...readability seems far more important. However, you mention programs where this syntax makes things easy...I would really like to see some examples. It might help me understand > what you're coming from. > > BTW, I think that if we're not going to use inclusive ranges on BOTH sides, then > R. Borogoves idea ( a = b[3@4] ) or something like it would be far superior. > > First/Last - intuitive > Start/Len - intuitive > First/Last+1 - might be easy to type, but not intuitive :( > > Walter wrote: > > > "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BFA97A0.66C04CBE@deming-os.org... > > > But we're talking about slicing....if you want a 0 length array, then just > > > declare one! > > > int[0] a; > > > > A 0 length array should be a valid slice. > > > > > This whole exclusive/inclusive thing is, IMHO, *very* non-intuitive. > > -- > The Villagers are Online! villagersonline.com > > .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] > .[ (a version.of(English).(precise.more)) is(possible) ] > ?[ you want.to(help(develop(it))) ] > > |
Copyright © 1999-2021 by the D Language Foundation