Thread overview
slicing mod proposal
Aug 20, 2004
h3r3tic
Aug 20, 2004
Derek Parnell
Aug 20, 2004
h3r3tic
Sep 26, 2004
Sha Chancellor
Sep 26, 2004
h3r3tic
Aug 20, 2004
Walter
August 20, 2004
just a short note:
now we can do:
(dmd 0.99)

int[] b = a[0..length-2];

where a is another array of int...


but isn't the 'length' ident redundant at all ? Python makes it implicit when the index is negative, so that a[0..-2] is a[0..length-2]. why dont we have it the same way ? it is very handy :)
August 20, 2004
On Fri, 20 Aug 2004 04:46:47 +0200, h3r3tic wrote:

> just a short note:
> now we can do:
> (dmd 0.99)
> 
> int[] b = a[0..length-2];
> 
> where a is another array of int...
> 
> but isn't the 'length' ident redundant at all ? Python makes it implicit when the index is negative, so that a[0..-2] is a[0..length-2]. why dont we have it the same way ? it is very handy :)

Its more explicit; tells the reader what we were intending to do.

It helps trap some mistakes...

   int x,y;

   y = calc_size(a, z);
   . . .
   x = y - 1;

   b = a[1..x];

But if for some reason 'y' was zero or lower, this would be an error. It might not go detected if -ve values were allowed in slices.

-- 
Derek
Melbourne, Australia
20/Aug/04 12:50:38 PM
August 20, 2004
Derek Parnell wrote:
> Its more explicit; tells the reader what we were intending to do.
> 
> It helps trap some mistakes...
> 
>    int x,y;
> 
>    y = calc_size(a, z);
>    . . .    x = y - 1;
> 
>    b = a[1..x];
> 
> But if for some reason 'y' was zero or lower, this would be an error. It
> might not go detected if -ve values were allowed in slices.
> 

ok, i get ur point. thanks :]

how about another thing though ? in python (yea, again ;]) i can say x[0:10:2] and that will create a sliced array with indexes 0, 2, 4, 6, 8, basically changing the stride of the slice. how bout that ? could be [a..b:c] or just [a..b..c] but i'd prefer the former
August 20, 2004
"h3r3tic" <h3r3tic@dev.null> wrote in message news:cg3omi$2p2q$1@digitaldaemon.com...
> just a short note:
> now we can do:
> (dmd 0.99)
>
> int[] b = a[0..length-2];
>
> where a is another array of int...
>
>
> but isn't the 'length' ident redundant at all ? Python makes it implicit when the index is negative, so that a[0..-2] is a[0..length-2]. why dont we have it the same way ? it is very handy :)

This is a common request. One reason it isn't done is it requires a runtime check, and people really like their arrays to be fast!


September 26, 2004
In article <cg3rb4$2qij$1@digitaldaemon.com>,
 h3r3tic <h3r3tic@dev.null> wrote:

> ok, i get ur point. thanks :]
> 
> how about another thing though ? in python (yea, again ;]) i can say x[0:10:2] and that will create a sliced array with indexes 0, 2, 4, 6, 8, basically changing the stride of the slice. how bout that ? could be [a..b:c] or just [a..b..c] but i'd prefer the former

Slices return a pointer to a contiguous block of memory located inside the array you sliced.  It doesn't allocated any new memory, it's just a pointer and a length.   At least that's my understanding.
September 26, 2004
Sha Chancellor wrote:
> In article <cg3rb4$2qij$1@digitaldaemon.com>,
>  h3r3tic <h3r3tic@dev.null> wrote:
> 
> 
>>ok, i get ur point. thanks :]
>>
>>how about another thing though ? in python (yea, again ;]) i can say x[0:10:2] and that will create a sliced array with indexes 0, 2, 4, 6, 8, basically changing the stride of the slice. how bout that ? could be [a..b:c] or just [a..b..c] but i'd prefer the former
> 
> 
> Slices return a pointer to a contiguous block of memory located inside the array you sliced.  It doesn't allocated any new memory, it's just a pointer and a length.   At least that's my understanding.

Sure, but there was a discussion going around some time earlier (relative to my old post) proposing interleaved arrays (even for multidimensional arrays) and this would just fit nicely with that concept.