Re: Suggestion: array slice through arr[base, size]

2015-02-10 Thread deadalnix via Digitalmars-d
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);


Re: Suggestion: array slice through arr[base, size]

2015-02-08 Thread karl via Digitalmars-d

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.


Re: Suggestion: array slice through arr[base, size]

2015-02-08 Thread Zach the Mystic via Digitalmars-d
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!


Suggestion: array slice through arr[base, size]

2015-02-08 Thread karl via Digitalmars-d

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.


Re: Suggestion: array slice through arr[base, size]

2015-02-08 Thread Orvid King via Digitalmars-d

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.


Re: Suggestion: array slice through arr[base, size]

2015-02-08 Thread Vladimir Panteleev via Digitalmars-d

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];


Re: Suggestion: array slice through arr[base, size]

2015-02-08 Thread Xinok via Digitalmars-d

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]);