digitalmars.D - Suggestion: array slice through arr[base, size]
- karl (7/7) Feb 08 2015 Hi, it's a bit unwieldy to write/read this:
- Orvid King (3/10) Feb 08 2015 No, because that looks like it should be indexing a uniform
- karl (4/6) Feb 08 2015 Oh, I didn't notice that conflict. So ok, disregard this
- Vladimir Panteleev (3/7) Feb 08 2015 If you don't want to write "base" twice, you can write:
- Zach the Mystic (3/10) Feb 08 2015 That's really interesting!
- deadalnix (8/15) Feb 10 2015 And if that scares you:
- Xinok (9/16) Feb 08 2015 If this is a common pattern in your code, you could write your
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
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
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
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
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:That's really interesting!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
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: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);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 10 2015
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