December 02, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9uc9lb$24ng$1@digitaldaemon.com... > Think about it this way. We declare an array as: > > int array[max]; > > loop through it as: > > for (i = 0; i < array.length; i++) > > wouldn't it make sense to slice it as [0 .. array.length]? On other hand, we could loop through it as: for (i = 0; i <= array.length - 1; i++) IMHO this actually is a proper way to scan through an array (but nobody actually uses it, including me =)). So woudln't it make sence to slice it as [0 .. array.length-1]? Your arguments are all based on the fact that D arrays are zero-based. I believe this is not really related to the way slicing operator should work, which is common to many languages. In fact, I've never seen exclusive slice ranges before (don't tell about C++ iterator-defined ranges - the thing is, iterators serve for many purposes other than defining a range)... |
December 02, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:9ucpd3$2nc8$1@digitaldaemon.com... > Your arguments are all based on the fact that D arrays are zero-based. Yes, and the routine way the length of an array is expressed. > I believe this is not really related to the way > slicing operator should work, which is common to many languages. I think it's important to be able to specify a 0 length slice without resorting to kludges like a negative difference is really 0 length. (Which also entails extra runtime overhead.) |
December 02, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Inclusive: a[0..a.length-1] It's intuitive, but difficult to express a zero length slice. Plus the user has to specify length-1 and the compiler has to increment it back again. Should be optimizable away but still a minor drawback. Runtime range checking in debug builds will prevent bugs where the user thought it was exclusive and accidentally specified a[0..a.length]. Exclusive: a[0..a.length] Easy to express a zero length slice. No additional runtime overhead. Not very intuitive however. Sure to be a major source of bugs, which will not be able to be caught by the debug runtime range checking. In that case, since there seems to be no winner in the debate between inclusive and exclusive ranges, I'd like to suggest going with the start/length approach instead. It doesn't go well with the [0..x] syntax however, that would be misleading. So we'd want a new syntax to denote a start/length array slice. I don't have any ideas except maybe a[0 @ a.length] Start/Length: a[0 @ a.length] It's intuitive, easy to express zero length slice, easy to tell it's not a range (C programmers will say "what the hell is that '@' for?" and look it up in the docs) Sometimes I hate the C comma operator... Prevents using syntax that alot of other languages use. I hardly ever need the comma operator, really. Just for wierd tricks, but those could be done another way. ;) Sean "Walter" <walter@digitalmars.com> wrote in message news:9uctpj$2rvp$2@digitaldaemon.com... > > "Pavel Minayev" <evilone@omen.ru> wrote in message news:9ucpd3$2nc8$1@digitaldaemon.com... > > Your arguments are all based on the fact that D arrays are zero-based. > > Yes, and the routine way the length of an array is expressed. > > > I believe this is not really related to the way > > slicing operator should work, which is common to many languages. > > I think it's important to be able to specify a 0 length slice without resorting to kludges like a negative difference is really 0 length. (Which also entails extra runtime overhead.) |
December 03, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | With all the bad vibes Walter is getting over this I felt I had to pipe
in with a word of support. I can really provide a better argument than
Walter except to say I don't find it all that confusing. When I first
saw the syntax in the spec I was afraid it was going to be inclusive and
was pleasantly surprised to fine it wasn't. You simply give the index
of the first element you wish to include and the index of the first
element you want to exclude. I guess I also like how it mirror the for
loop expression of traversing and array slice. I liked it in C++ and I
like it better in D since it is the language that supports it directly.
I will say I like the idea of the start and length syntax for slicing
too, but the syntax will need some work if it is not to be an eye/mind
sore.
Dan
Walter wrote:
>
> "Pavel Minayev" <evilone@omen.ru> wrote in message news:9ucpd3$2nc8$1@digitaldaemon.com...
> > Your arguments are all based on the fact that D arrays are zero-based.
>
> Yes, and the routine way the length of an array is expressed.
>
> > I believe this is not really related to the way
> > slicing operator should work, which is common to many languages.
>
> I think it's important to be able to specify a 0 length slice without resorting to kludges like a negative difference is really 0 length. (Which also entails extra runtime overhead.)
|
December 03, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to a | Some thoughts on array slicing ranges -
There are two points to consider, namely semantics and syntax. The semantics chosen (including the first element and excluding the last element) fits in with how C arrays are specified, how for loops are usually written, etc. So I think these semantics make pretty good sense. (This is assuming that only one semantic for slicing ranges is included - there could be more than one.)
Syntax is another issue. The problem with using []'s is that it "looks like" both ends are included. The Mesa programming language had an interesting notation for Integer subranges, which was, IIRC, one of the following - all the following ranges are identical -
INTEGER [ 1 .. 10 ]
INTEGER [ 1 .. 11 )
INTEGER ( 0 .. 10 ]
INTEGER ( 0 .. 11 )
I'm sure you can see what's going on - the ()'s are used for "open" endpoints, the []'s are used for "closed" endpoints. The resulting syntax was, I thought, pretty intuitive (perhaps because of background in mathematics).
Maybe a syntax something along these lines could be adopted in D.
a <a@b.c> writes:
> With all the bad vibes Walter is getting over this I felt I had to pipe
> in with a word of support. I can really provide a better argument than
> Walter except to say I don't find it all that confusing. When I first
> saw the syntax in the spec I was afraid it was going to be inclusive and
> was pleasantly surprised to fine it wasn't. You simply give the index
> of the first element you wish to include and the index of the first
> element you want to exclude. I guess I also like how it mirror the for
> loop expression of traversing and array slice. I liked it in C++ and I
> like it better in D since it is the language that supports it directly.
> I will say I like the idea of the start and length syntax for slicing
> too, but the syntax will need some work if it is not to be an eye/mind
> sore.
>
> Dan
>
> Walter wrote:
> >
> > "Pavel Minayev" <evilone@omen.ru> wrote in message news:9ucpd3$2nc8$1@digitaldaemon.com...
> > > Your arguments are all based on the fact that D arrays are zero-based.
> >
> > Yes, and the routine way the length of an array is expressed.
> >
> > > I believe this is not really related to the way
> > > slicing operator should work, which is common to many languages.
> >
> > I think it's important to be able to specify a 0 length slice without resorting to kludges like a negative difference is really 0 length. (Which also entails extra runtime overhead.)
|
December 03, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote:
> Subject:
>
> Re: array slicing ranges
> From:
>
> "Pavel Minayev" <evilone@omen.ru>
> Date:
>
> Sun, 2 Dec 2001 11:39:55 +0300
>
> Newsgroups:
>
> D
>
>
> "Walter" <walter@digitalmars.com> wrote in message
> news:9uc9lb$24ng$1@digitaldaemon.com...
>
>
>>> Think about it this way. We declare an array as:
>>>
>>> int array[max];
>>>
>>> loop through it as:
>>>
>>> for (i = 0; i < array.length; i++)
>>>
>>> wouldn't it make sense to slice it as [0 .. array.length]?
>>
>
> On other hand, we could loop through it as:
>
> for (i = 0; i <= array.length - 1; i++)
>
> IMHO this actually is a proper way to scan through an
> array (but nobody actually uses it, including me =)).
>
> So woudln't it make sence to slice it as [0 .. array.length-1]?
...
Perhaps, as was suggested earlier, there should be an array.maxIndex value? Length is reasonable for the number of elements, but doesn't really match the maximum index. And a fully closed interval is easier to grasp. So one would write:
a [0 .. a.maxIndex]
(which would be equal to a.length - 1, as long as we agree to use zero based indexing [and if someday it is generalized to n-based indexing, only a minIndex value would need to be added]).
|
December 04, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Hixson | "Charles Hixson" <charleshixsn@earthlink.net> wrote in message news:3C0C0732.6030108@earthlink.net... > Perhaps, as was suggested earlier, there should be an > array.maxIndex value? Length is reasonable for the number of > elements, but doesn't really match the maximum index. And a > fully closed interval is easier to grasp. So one would write: > a [0 .. a.maxIndex] > (which would be equal to a.length - 1, as long as we agree to > use zero based indexing [and if someday it is generalized to > n-based indexing, only a minIndex value would need to be added]). What would maxIndex be for a 0 length array? |
December 04, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to la7y6nvo | <la7y6nvo@shamko.com> wrote in message news:s7citbotd5u.fsf@michael.shamko.com... > INTEGER [ 1 .. 10 ] > INTEGER [ 1 .. 11 ) > INTEGER ( 0 .. 10 ] > INTEGER ( 0 .. 11 ) > > I'm sure you can see what's going on - the ()'s are used for "open" endpoints, the []'s are used for "closed" endpoints. The resulting syntax was, I thought, pretty intuitive (perhaps because of background in mathematics). > > Maybe a syntax something along these lines could be adopted in D. While that can be done, I'm a little uncomfortable with: 1. the non-matching ( and ] <g>. 2. ] and ) look pretty similar on the screen. |
December 04, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9uhjh8$1fhs$2@digitaldaemon.com... > > <la7y6nvo@shamko.com> wrote in message news:s7citbotd5u.fsf@michael.shamko.com... > > INTEGER [ 1 .. 10 ] > > INTEGER [ 1 .. 11 ) > > INTEGER ( 0 .. 10 ] > > INTEGER ( 0 .. 11 ) > > > > I'm sure you can see what's going on - the ()'s are used for "open" endpoints, the []'s are used for "closed" endpoints. The resulting syntax was, I thought, pretty intuitive (perhaps because of background in mathematics). > > > > Maybe a syntax something along these lines could be adopted in D. > > While that can be done, I'm a little uncomfortable with: > 1. the non-matching ( and ] <g>. > 2. ] and ) look pretty similar on the screen. Still... I really like the idea. The syntax is quite intuitive to everybody who knows math a little bit. And you can choose what you want to include and what you don't. IMHO the best syntax proposal on the topic so far. I vote for! |
December 04, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9uhjh7$1fhs$1@digitaldaemon.com... > What would maxIndex be for a 0 length array? maxIndex = length - 1 = 0 - 1 = -1 |
Copyright © 1999-2021 by the D Language Foundation