digitalmars.D - opCaret to complement opDollar when specifying slices
- Dario Schiavon (20/20) Jun 02 2012 Hi,
- Steven Schveighoffer (7/26) Jun 04 2012 I suggested this, and it was shot down rather pointedly by Walter (with ...
- Mehrdad (3/34) Jun 04 2012 Can you use "null"?
- Steven Schveighoffer (4/37) Jun 04 2012 Hm... now that null has its own type, I likely could.
- Roman D. Boiko (4/9) Jun 04 2012 But if the key is non-nullable this might be confusing, or even
- Steven Schveighoffer (10/21) Jun 04 2012 Well, you could say that null keys are not allowed. But then it makes n...
- Xinok (7/28) Jun 04 2012 The problem I see with this, it would be a larger burden when
- Steven Schveighoffer (7/35) Jun 06 2012 caret would have to map to 0 for slices and containers who already have ...
- Gor Gyolchanyan (22/59) Jun 06 2012 k
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
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
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:Can you use "null"?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
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:Hm... now that null has its own type, I likely could. I suppose that would map properly to 0. -SteveOn Sat, 02 Jun 2012 07:49:16 -0400, Dario Schiavon <dario.schiavon gmail.com> wrote:Can you use "null"?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
Jun 04 2012
On Monday, 4 June 2012 at 20:26:52 UTC, Steven Schveighoffer wrote:On Mon, 04 Jun 2012 16:13:49 -0400, MehrdadBut if the key is non-nullable this might be confusing, or even not possible.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
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: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... -SteveOn Mon, 04 Jun 2012 16:13:49 -0400, MehrdadBut if the key is non-nullable this might be confusing, or even not possible.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
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
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: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. -SteveHi, 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 06 2012
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:kOn 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=s thefor 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 .. =akewould 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=rray?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=ySince the $ sign was evidently chosen to parallel the regexp syntax, wh=$]don't we add ^ to refer to the first element? This way, collection[^ ..=aswould 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=eastthe opDollar, so I understand if you oppose the idea, but it would at l=ssmake 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=r*.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 =but donot start on 0. =C2=A0The only one I know of at this point is Red Black T=ree.-SteveCircular 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