www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion: array slice through arr[base, size]

reply "karl" <ultrano hotmail.com> writes:
Hi, it's a bit unwieldy to write/read this:
result = src[base .. base+size];
instead of:
result = src[base, size];

Maybe such syntax would be a welcome addition to D? I don't see 
it conflicting with the existing grammar, and only the 2 
slicing-operators need further extending to support it.
Feb 08 2015
next sibling parent reply "Orvid King" <blah38621 gmail.com> writes:
On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
 Hi, it's a bit unwieldy to write/read this:
 result = src[base .. base+size];
 instead of:
 result = src[base, size];

 Maybe such syntax would be a welcome addition to D? I don't see 
 it conflicting with the existing grammar, and only the 2 
 slicing-operators need further extending to support it.
No, because that looks like it should be indexing a uniform multi-dimensional array, which it very definitely is not.
Feb 08 2015
parent "karl" <ultrano hotmail.com> writes:
On Sunday, 8 February 2015 at 15:23:13 UTC, Orvid King wrote:
 No, because that looks like it should be indexing a uniform 
 multi-dimensional array, which it very definitely is not.
Oh, I didn't notice that conflict. So ok, disregard this suggestion. I'll just use methods like the .slice(int,int) one suggested above instead.
Feb 08 2015
prev sibling next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
 Hi, it's a bit unwieldy to write/read this:
 result = src[base .. base+size];
 instead of:
 result = src[base, size];
If you don't want to write "base" twice, you can write: result = src[base..$][0..size];
Feb 08 2015
next sibling parent "Zach the Mystic" <reachzach gggmail.com> writes:
On Sunday, 8 February 2015 at 15:28:10 UTC, Vladimir Panteleev 
wrote:
 On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
 Hi, it's a bit unwieldy to write/read this:
 result = src[base .. base+size];
 instead of:
 result = src[base, size];
If you don't want to write "base" twice, you can write: result = src[base..$][0..size];
That's really interesting!
Feb 08 2015
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Sunday, 8 February 2015 at 15:28:10 UTC, Vladimir Panteleev 
wrote:
 On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
 Hi, it's a bit unwieldy to write/read this:
 result = src[base .. base+size];
 instead of:
 result = src[base, size];
If you don't want to write "base" twice, you can write: result = src[base..$][0..size];
And if that scares you: auto slice(T)(T[] t, size_t start, size_t len) { return t[base .. $][0 .. len]; } int[] arr; arr.slice(3, 5);
Feb 10 2015
prev sibling parent "Xinok" <xinok live.com> writes:
On Sunday, 8 February 2015 at 15:20:17 UTC, karl wrote:
 Hi, it's a bit unwieldy to write/read this:
 result = src[base .. base+size];
 instead of:
 result = src[base, size];

 Maybe such syntax would be a welcome addition to D? I don't see 
 it conflicting with the existing grammar, and only the 2 
 slicing-operators need further extending to support it.
If this is a common pattern in your code, you could write your own function to do this and call it using UFCS: Range slice(Range)(Range r, size_t base, size_t size) { return r[base .. base+size]; } auto arr = [0,1,2,3,4,5,6,7,8,9]; assert(arr.slice(4,2) == [4,5]);
Feb 08 2015