Thread overview
Synax of array slices.
Nov 29, 2005
Andrew Fedoniouk
Nov 29, 2005
Derek Parnell
Nov 29, 2005
Andrew Fedoniouk
Nov 29, 2005
Derek Parnell
Nov 29, 2005
Hasan Aljudy
Nov 29, 2005
Bruno Medeiros
Nov 29, 2005
Derek Parnell
Dec 03, 2005
F
Dec 03, 2005
Ben Hinkle
November 29, 2005
Hi, gentlemen,

I've implemented in my script engine (close to ECMAScript)
array slices (inspired by D, thanks).  While doing this I came up
with the idea of using four forms of slicing syntax:

a[s..e] // standard, 's' and 'e' are as in D
a[s..]   //  'e' will be substituted by a.length
a[..e]  //  's' will be substituted by 0
a[..]  //  ...

I am pretty sure this idea ($, a.length wars) was discussed already but
cannot find it.
Can you see any problems with this notation?
If there is no major issues then probably makes sense  to consider it in D?

Andrew Fedoniouk.
http://terrainformatica.com

PS. TIScript lives on http://terrainformatica.com/tiscript/










November 29, 2005
On Mon, 28 Nov 2005 16:19:51 -0800, Andrew Fedoniouk wrote:

> Hi, gentlemen,
> 
> I've implemented in my script engine (close to ECMAScript)
> array slices (inspired by D, thanks).  While doing this I came up
> with the idea of using four forms of slicing syntax:
> 
> a[s..e] // standard, 's' and 'e' are as in D
> a[s..]   //  'e' will be substituted by a.length
> a[..e]  //  's' will be substituted by 0
> a[..]  //  ...
> 
> I am pretty sure this idea ($, a.length wars) was discussed already but
> cannot find it.
> Can you see any problems with this notation?
> If there is no major issues then probably makes sense  to consider it in D?

The double dots without a context can be harder to see than when they have
a context.
How is the idiom $-n meant to be shown?
A incorrectly missing identifier is harder to detect as a mistake.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
29/11/2005 11:39:08 AM
November 29, 2005
>>
>> a[s..e] // standard, 's' and 'e' are as in D
>> a[s..]   //  'e' will be substituted by a.length
>> a[..e]  //  's' will be substituted by 0
>> a[..]  //  ...
>>
>> Can you see any problems with this notation?
>
> The double dots without a context can be harder to see than when they have
> a context.
> How is the idiom $-n meant to be shown?

I am assuming that the most frequent use case is sort of
a[n..]  (  a[n..a.length]  )

I beleive that in cases when you need a[ 0 .. $-n ]
a[ 0 .. a.length - n ] will look better.

> A incorrectly missing identifier is harder to detect as a mistake.
>

In complex cases $ is also not good too I beleive:

a[0..lastNonWS($)] - will it work or not, btw?
a[0..b[0..$].length] - what was ciphered here?





November 29, 2005
On Mon, 28 Nov 2005 16:53:09 -0800, Andrew Fedoniouk wrote:

>>>
>>> a[s..e] // standard, 's' and 'e' are as in D
>>> a[s..]   //  'e' will be substituted by a.length
>>> a[..e]  //  's' will be substituted by 0
>>> a[..]  //  ...
>>>
>>> Can you see any problems with this notation?
>>
>> The double dots without a context can be harder to see than when they have
>> a context.
>> How is the idiom $-n meant to be shown?
> 
> I am assuming that the most frequent use case is sort of
> a[n..]  (  a[n..a.length]  )
> 
> I beleive that in cases when you need a[ 0 .. $-n ]
> a[ 0 .. a.length - n ] will look better.
> 
>> A incorrectly missing identifier is harder to detect as a mistake.
>>
> 
> In complex cases $ is also not good too I beleive:

And I believe the opposite ;-)

> a[0..lastNonWS($)] - will it work or not, btw?
No it will not work because '$' its innermost scope is not a slice.

> a[0..b[0..$].length] - what was ciphered here?
The innermost scope of the '$' is the slice of the array 'b'. Therefore a slice of 'b' is taken from 0 to $ and then this length is used in the slice of 'a'. As you can see, this example uses a redundant format as

  a[0 .. b.length]

would be sufficient.

However a better example might have been ...

  a[m..b[n..$].length]

which is alternatively written

  a[m..b[n..b.length].length]

If we use more typical identifier names ...

  vCustomers[lStartPos..Sort.Bias[lUserChoice..Sort.Bias.length].length]

and we start to see dots before our eyes ;-)

Whereas

  vCustomers[lStartPos..Sort.Bias[lUserChoice..$].length]

is reduced clutter, IMHO.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
29/11/2005 11:55:39 AM
November 29, 2005
Derek Parnell wrote:
> On Mon, 28 Nov 2005 16:53:09 -0800, Andrew Fedoniouk wrote:
> 
> 
>>>>a[s..e] // standard, 's' and 'e' are as in D
>>>>a[s..]   //  'e' will be substituted by a.length
>>>>a[..e]  //  's' will be substituted by 0
>>>>a[..]  //  ...
>>>>
>>>>Can you see any problems with this notation?
>>>
>>>The double dots without a context can be harder to see than when they have
>>>a context.
>>>How is the idiom $-n meant to be shown?
>>
>>I am assuming that the most frequent use case is sort of
>>a[n..]  (  a[n..a.length]  )
>>
>>I beleive that in cases when you need a[ 0 .. $-n ]
>>a[ 0 .. a.length - n ] will look better.
>>
>>
>>>A incorrectly missing identifier is harder to detect as a mistake.
>>>
>>
>>In complex cases $ is also not good too I beleive:
> 
> 
> And I believe the opposite ;-)
> 
> 
>>a[0..lastNonWS($)] - will it work or not, btw?
> 
> No it will not work because '$' its innermost scope is not a slice.
> 
> 
>>a[0..b[0..$].length] - what was ciphered here?
> 
> The innermost scope of the '$' is the slice of the array 'b'. Therefore a
> slice of 'b' is taken from 0 to $ and then this length is used in the slice
> of 'a'. As you can see, this example uses a redundant format as 
> 
>   a[0 .. b.length] 
> 
> would be sufficient.
> 
> However a better example might have been ...
> 
>   a[m..b[n..$].length]
> 
> which is alternatively written 
> 
>   a[m..b[n..b.length].length]
> 
> If we use more typical identifier names ...
> 
>   vCustomers[lStartPos..Sort.Bias[lUserChoice..Sort.Bias.length].length]
> 
> and we start to see dots before our eyes ;-)
> 
> Whereas
> 
>   vCustomers[lStartPos..Sort.Bias[lUserChoice..$].length]
> 
> is reduced clutter, IMHO.
> 

IMHO this corner case doesn't really show much, because if you ask me, no one should code like that!! Wether you use $ or .length here doesn't make much differentce, it's confusing either way.
November 29, 2005
Derek Parnell wrote:
> 
>>a[0..lastNonWS($)] - will it work or not, btw?
> 
> No it will not work because '$' its innermost scope is not a slice.
> 

It works here. lastNonWS is just a regular function, right?

-- 
Bruno Medeiros - CS/E student
"Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
November 29, 2005
On Tue, 29 Nov 2005 11:16:30 +0000, Bruno Medeiros wrote:

> Derek Parnell wrote:
>> 
>>>a[0..lastNonWS($)] - will it work or not, btw?
>> 
>> No it will not work because '$' its innermost scope is not a slice.
>> 
> 
> It works here. lastNonWS is just a regular function, right?

Well I am surprised. I just tried it to and it works. I don't know if this is now a bug or not?

Walter?

-- 
Derek Parnell
Melbourne, Australia
30/11/2005 1:42:42 AM
December 03, 2005
What about:

a[0..end]; //everything
a[..]; //also everything
a[k1..end-k2]; //a slice (redefining end here is not difficult, I think, is
meaningfull, too)

a[..,k1..end-k2,..]; //slicing over the second dim (end has the correspondent
meaning)

length of a will be end+1 then (no meaning outside [])

Opinions?




In article <4m61pv28t205$.1qh5v1k9041jy$.dlg@40tude.net>, Derek Parnell says...
>
>On Mon, 28 Nov 2005 16:19:51 -0800, Andrew Fedoniouk wrote:
>
>> Hi, gentlemen,
>> 
>> I've implemented in my script engine (close to ECMAScript)
>> array slices (inspired by D, thanks).  While doing this I came up
>> with the idea of using four forms of slicing syntax:
>> 
>> a[s..e] // standard, 's' and 'e' are as in D
>> a[s..]   //  'e' will be substituted by a.length
>> a[..e]  //  's' will be substituted by 0
>> a[..]  //  ...
>> 
>> I am pretty sure this idea ($, a.length wars) was discussed already but
>> cannot find it.
>> Can you see any problems with this notation?
>> If there is no major issues then probably makes sense  to consider it in D?
>
>The double dots without a context can be harder to see than when they have
>a context.
>How is the idiom $-n meant to be shown?
>A incorrectly missing identifier is harder to detect as a mistake.
>
>-- 
>Derek
>(skype: derek.j.parnell)
>Melbourne, Australia
>29/11/2005 11:39:08 AM


December 03, 2005
"F" <F_member@pathlink.com> wrote in message news:dmsrfe$mhq$1@digitaldaemon.com...
> What about:
>
> a[0..end]; //everything
> a[..]; //also everything
> a[k1..end-k2]; //a slice (redefining end here is not difficult, I think,
> is
> meaningfull, too)
>
> a[..,k1..end-k2,..]; //slicing over the second dim (end has the
> correspondent
> meaning)
>
> length of a will be end+1 then (no meaning outside [])
>
> Opinions?

MATLAB uses 'end' as well. In my experimental "Cx" language that I've been playing around with I chose 'end', too, with the following behavior: if the identifier 'end' is undefined in an indexing expression then it is replaced with the length. I don't remember if that's the same behavior as D's 'length'. I also took MATLAB's slicing syntax a:b instead of D's a..b. So in Cx to slice from position i to the end of x one would write x[i:end]. Maybe I'm just used to MATLAB but I find x[i:end] more readable than x[i..$].