www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opCaret to complement opDollar when specifying slices

reply "Dario Schiavon" <dario.schiavon gmail.com> writes:
Hi,

I just read some old threads about opDollar and the wish to have 
it work for non zero-based arrays, arrays with gaps, associative 
arrays with non-numerical indices, and so on. It was suggested to 
define opDollar as the end of the array rather than the length 
(and perhaps rename opDollar to opEnd to reflect this 
interpretation), so that collection[someIndex .. $] would 
consistently refer to a slice from someIndex to the end of the 
collection (of course the keys must have a defined ordering for 
it to make sense).

I'm just thinking, if we want to generalize slices for those 
cases, shouldn't we have a symmetrical operator for the first 
element of the array? Since the $ sign was evidently chosen to 
parallel the regexp syntax, why don't we add ^ to refer to the 
first element? This way, collection[^ .. $] would slice the 
entire collection, just like collection[].

Until now, ^ is only used as a binary operator, so this addition 
shouldn't lead to ambiguous syntax. It surely wouldn't be used as 
often as the opDollar, so I understand if you oppose the idea, 
but it would at least make the language a little more "complete".
Jun 02 2012
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 02 Jun 2012 07:49:16 -0400, Dario Schiavon  
<dario.schiavon gmail.com> wrote:

 Hi,

 I just read some old threads about opDollar and the wish to have it work  
 for non zero-based arrays, arrays with gaps, associative arrays with  
 non-numerical indices, and so on. It was suggested to define opDollar as  
 the end of the array rather than the length (and perhaps rename opDollar  
 to opEnd to reflect this interpretation), so that collection[someIndex  
 .. $] would consistently refer to a slice from someIndex to the end of  
 the collection (of course the keys must have a defined ordering for it  
 to make sense).

 I'm just thinking, if we want to generalize slices for those cases,  
 shouldn't we have a symmetrical operator for the first element of the  
 array? Since the $ sign was evidently chosen to parallel the regexp  
 syntax, why don't we add ^ to refer to the first element? This way,  
 collection[^ .. $] would slice the entire collection, just like  
 collection[].

 Until now, ^ is only used as a binary operator, so this addition  
 shouldn't lead to ambiguous syntax. It surely wouldn't be used as often  
 as the opDollar, so I understand if you oppose the idea, but it would at  
 least make the language a little more "complete".

I suggested this, and it was shot down rather pointedly by Walter (with not very convincing arguments I might add). Probably not much chance of success. http://forum.dlang.org/post/op.vco5zwhreav7ka localhost.localdomain -Steve
Jun 04 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Monday, 4 June 2012 at 19:55:49 UTC, Steven Schveighoffer 
wrote:
 On Sat, 02 Jun 2012 07:49:16 -0400, Dario Schiavon 
 <dario.schiavon gmail.com> wrote:

 Hi,

 I just read some old threads about opDollar and the wish to 
 have it work for non zero-based arrays, arrays with gaps, 
 associative arrays with non-numerical indices, and so on. It 
 was suggested to define opDollar as the end of the array 
 rather than the length (and perhaps rename opDollar to opEnd 
 to reflect this interpretation), so that collection[someIndex 
 .. $] would consistently refer to a slice from someIndex to 
 the end of the collection (of course the keys must have a 
 defined ordering for it to make sense).

 I'm just thinking, if we want to generalize slices for those 
 cases, shouldn't we have a symmetrical operator for the first 
 element of the array? Since the $ sign was evidently chosen to 
 parallel the regexp syntax, why don't we add ^ to refer to the 
 first element? This way, collection[^ .. $] would slice the 
 entire collection, just like collection[].

 Until now, ^ is only used as a binary operator, so this 
 addition shouldn't lead to ambiguous syntax. It surely 
 wouldn't be used as often as the opDollar, so I understand if 
 you oppose the idea, but it would at least make the language a 
 little more "complete".

I suggested this, and it was shot down rather pointedly by Walter (with not very convincing arguments I might add). Probably not much chance of success. http://forum.dlang.org/post/op.vco5zwhreav7ka localhost.localdomain -Steve

Can you use "null"?
Jun 04 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Jun 2012 16:13:49 -0400, Mehrdad <wfunction hotmail.com> wrote:

 On Monday, 4 June 2012 at 19:55:49 UTC, Steven Schveighoffer wrote:
 On Sat, 02 Jun 2012 07:49:16 -0400, Dario Schiavon  
 <dario.schiavon gmail.com> wrote:

 Hi,

 I just read some old threads about opDollar and the wish to have it  
 work for non zero-based arrays, arrays with gaps, associative arrays  
 with non-numerical indices, and so on. It was suggested to define  
 opDollar as the end of the array rather than the length (and perhaps  
 rename opDollar to opEnd to reflect this interpretation), so that  
 collection[someIndex .. $] would consistently refer to a slice from  
 someIndex to the end of the collection (of course the keys must have a  
 defined ordering for it to make sense).

 I'm just thinking, if we want to generalize slices for those cases,  
 shouldn't we have a symmetrical operator for the first element of the  
 array? Since the $ sign was evidently chosen to parallel the regexp  
 syntax, why don't we add ^ to refer to the first element? This way,  
 collection[^ .. $] would slice the entire collection, just like  
 collection[].

 Until now, ^ is only used as a binary operator, so this addition  
 shouldn't lead to ambiguous syntax. It surely wouldn't be used as  
 often as the opDollar, so I understand if you oppose the idea, but it  
 would at least make the language a little more "complete".

I suggested this, and it was shot down rather pointedly by Walter (with not very convincing arguments I might add). Probably not much chance of success. http://forum.dlang.org/post/op.vco5zwhreav7ka localhost.localdomain

Can you use "null"?

Hm... now that null has its own type, I likely could. I suppose that would map properly to 0. -Steve
Jun 04 2012
prev sibling next sibling parent "Roman D. Boiko" <rb d-coding.com> writes:
On Monday, 4 June 2012 at 20:26:52 UTC, Steven Schveighoffer 
wrote:
 On Mon, 04 Jun 2012 16:13:49 -0400, Mehrdad
 Can you use "null"?

Hm... now that null has its own type, I likely could. I suppose that would map properly to 0. -Steve

not possible.
Jun 04 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Jun 2012 16:32:55 -0400, Roman D. Boiko <rb d-coding.com> wrote:

 On Monday, 4 June 2012 at 20:26:52 UTC, Steven Schveighoffer wrote:
 On Mon, 04 Jun 2012 16:13:49 -0400, Mehrdad
 Can you use "null"?

Hm... now that null has its own type, I likely could. I suppose that would map properly to 0. -Steve

possible.

Well, you could say that null keys are not allowed. But then it makes no sense for null not to work in other places. I really am not sure this works well, I think it would be too confusing. map[null] = 5; // set the first element to 5? map[null..4] = 6; // set all the elements with keys before 4 to 6 map!keyType k = null; map[k..$]; // likely an error. I really actually think I like using map.begin better than null... -Steve
Jun 04 2012
prev sibling next sibling parent "Xinok" <xinok live.com> writes:
On Saturday, 2 June 2012 at 11:49:17 UTC, Dario Schiavon wrote:
 Hi,

 I just read some old threads about opDollar and the wish to 
 have it work for non zero-based arrays, arrays with gaps, 
 associative arrays with non-numerical indices, and so on. It 
 was suggested to define opDollar as the end of the array rather 
 than the length (and perhaps rename opDollar to opEnd to 
 reflect this interpretation), so that collection[someIndex .. 
 $] would consistently refer to a slice from someIndex to the 
 end of the collection (of course the keys must have a defined 
 ordering for it to make sense).

 I'm just thinking, if we want to generalize slices for those 
 cases, shouldn't we have a symmetrical operator for the first 
 element of the array? Since the $ sign was evidently chosen to 
 parallel the regexp syntax, why don't we add ^ to refer to the 
 first element? This way, collection[^ .. $] would slice the 
 entire collection, just like collection[].

 Until now, ^ is only used as a binary operator, so this 
 addition shouldn't lead to ambiguous syntax. It surely wouldn't 
 be used as often as the opDollar, so I understand if you oppose 
 the idea, but it would at least make the language a little more 
 "complete".

The problem I see with this, it would be a larger burden when writing generic code. Libraries would have to be written to compensate for those containers. I'd prefer that all containers are simply zero-based, unless there's a need for negative indices (i.e. pointers). I think random-access ranges may be intended to be zero-based as well.
Jun 04 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Jun 2012 17:23:57 -0400, Xinok <xinok live.com> wrote:

 On Saturday, 2 June 2012 at 11:49:17 UTC, Dario Schiavon wrote:
 Hi,

 I just read some old threads about opDollar and the wish to have it  
 work for non zero-based arrays, arrays with gaps, associative arrays  
 with non-numerical indices, and so on. It was suggested to define  
 opDollar as the end of the array rather than the length (and perhaps  
 rename opDollar to opEnd to reflect this interpretation), so that  
 collection[someIndex .. $] would consistently refer to a slice from  
 someIndex to the end of the collection (of course the keys must have a  
 defined ordering for it to make sense).

 I'm just thinking, if we want to generalize slices for those cases,  
 shouldn't we have a symmetrical operator for the first element of the  
 array? Since the $ sign was evidently chosen to parallel the regexp  
 syntax, why don't we add ^ to refer to the first element? This way,  
 collection[^ .. $] would slice the entire collection, just like  
 collection[].

 Until now, ^ is only used as a binary operator, so this addition  
 shouldn't lead to ambiguous syntax. It surely wouldn't be used as often  
 as the opDollar, so I understand if you oppose the idea, but it would  
 at least make the language a little more "complete".

The problem I see with this, it would be a larger burden when writing generic code. Libraries would have to be written to compensate for those containers. I'd prefer that all containers are simply zero-based, unless there's a need for negative indices (i.e. pointers). I think random-access ranges may be intended to be zero-based as well.

caret would have to map to 0 for slices and containers who already have a notion of first element being zero. This would not be hard. And in fact, would make generic code *easier*. Right now, you have to special case containers who support slicing but do not start on 0. The only one I know of at this point is Red Black Tree. -Steve
Jun 06 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
On Wed, Jun 6, 2012 at 3:53 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Mon, 04 Jun 2012 17:23:57 -0400, Xinok <xinok live.com> wrote:

 On Saturday, 2 June 2012 at 11:49:17 UTC, Dario Schiavon wrote:
 Hi,

 I just read some old threads about opDollar and the wish to have it wor=



 for non zero-based arrays, arrays with gaps, associative arrays with
 non-numerical indices, and so on. It was suggested to define opDollar a=



 end of the array rather than the length (and perhaps rename opDollar to
 opEnd to reflect this interpretation), so that collection[someIndex .. =



 would consistently refer to a slice from someIndex to the end of the
 collection (of course the keys must have a defined ordering for it to m=



 sense).

 I'm just thinking, if we want to generalize slices for those cases,
 shouldn't we have a symmetrical operator for the first element of the a=



 Since the $ sign was evidently chosen to parallel the regexp syntax, wh=



 don't we add ^ to refer to the first element? This way, collection[^ ..=



 would slice the entire collection, just like collection[].

 Until now, ^ is only used as a binary operator, so this addition
 shouldn't lead to ambiguous syntax. It surely wouldn't be used as often=



 the opDollar, so I understand if you oppose the idea, but it would at l=



 make the language a little more "complete".

The problem I see with this, it would be a larger burden when writing generic code. Libraries would have to be written to compensate for those containers. I'd prefer that all containers are simply zero-based, unless there's a need for negative indices (i.e. pointers). I think random-acce=


 ranges may be intended to be zero-based as well.

caret would have to map to 0 for slices and containers who already have a notion of first element being zero. This would not be hard. =C2=A0And in fact, would make generic code *easie=

 =C2=A0Right now, you have to special case containers who support slicing =

 not start on 0. =C2=A0The only one I know of at this point is Red Black T=

 -Steve

Circular buffers essentially start nowhere and end nowhere. Having a caret operator for slices would ease the manipulation of circular buffers and would allow one to "rotate" the buffer, essentially changing the cared and dollar value. --=20 Bye, Gor Gyolchanyan.
Jun 06 2012