www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - slicing mod proposal

reply h3r3tic <h3r3tic dev.null> writes:
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 :)
Aug 19 2004
next sibling parent reply Derek Parnell <derek psych.ward> writes:
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
Aug 19 2004
parent reply h3r3tic <h3r3tic dev.null> writes:
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
Aug 19 2004
parent reply Sha Chancellor <schancel pacific.net> writes:
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.
Sep 26 2004
parent h3r3tic <h3r3tic dev.null> writes:
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.
Sep 26 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"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!
Aug 19 2004