digitalmars.D.learn - Three questions on std.range
- Sam Hu (39/39) Jun 16 2009 Hello,
- Max Samukha (8/47) Jun 17 2009 3. This is a (hackish) way to detect if Range.empty's value is
- Sam Hu (4/11) Jun 17 2009 Thank you so much for all your help!
- Max Samukha (14/53) Jun 17 2009 Looks like a bug. The fix is easy:
Hello, Just get to my point to save time: 1.retro: void testRetro() { int[] arr=[1,2,3,4,5]; auto retArr=retro(arr); for(int i=0;i<retArr;i++) writef("%d ",retArr[i]); } void main() { testRetro; } output: 0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here? static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - n + 1]; } Per my understanding,the above will return the nth element of _input in reverse order. I try to count my fingers with length-n+1 but cannot get a correct result. 3. About isInfinte: template isInfinite(Range) { static if (isInputRange!(Range) && is(char[1 + Range.empty])) enum bool isInfinite = !Range.empty; else enum bool isInfinite = false; } In the is expression is(char[1+ Range.empty]),below is my understanding; Step 1: is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1]) Step 2: But what does this help with determining whether Range is infinite or not?say char[1+0],char[1+1]? Any help would be much much appreciated. Regards, Sam
Jun 16 2009
On Wed, 17 Jun 2009 00:04:51 -0400, Sam Hu <samhudotsamhu gmail.com> wrote:Hello, Just get to my point to save time: 1.retro: void testRetro() { int[] arr=[1,2,3,4,5]; auto retArr=retro(arr); for(int i=0;i<retArr;i++) writef("%d ",retArr[i]); } void main() { testRetro; } output: 0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here? static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - n + 1]; } Per my understanding,the above will return the nth element of _input in reverse order. I try to count my fingers with length-n+1 but cannot get a correct result. 3. About isInfinte: template isInfinite(Range) { static if (isInputRange!(Range) && is(char[1 + Range.empty])) enum bool isInfinite = !Range.empty; else enum bool isInfinite = false; } In the is expression is(char[1+ Range.empty]),below is my understanding; Step 1: is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1]) Step 2: But what does this help with determining whether Range is infinite or not?say char[1+0],char[1+1]? Any help would be much much appreciated. Regards, Sam3. This is a (hackish) way to detect if Range.empty's value is statically known. Infinite ranges must define a boolean 'empty' member that evaluates at compile time to false. Probably, there should be a more generic way to do it. Something like isCT(alias expression) template. I kinda have one but it doesn't work in all cases due to a compiler bug.
Jun 17 2009
Max Samukha Wrote:3. This is a (hackish) way to detect if Range.empty's value is statically known. Infinite ranges must define a boolean 'empty' member that evaluates at compile time to false. Probably, there should be a more generic way to do it. Something like isCT(alias expression) template. I kinda have one but it doesn't work in all cases due to a compiler bug.Thank you so much for all your help! Regards, Sam
Jun 17 2009
On Wed, 17 Jun 2009 00:04:51 -0400, Sam Hu <samhudotsamhu gmail.com> wrote:Hello, Just get to my point to save time: 1.retro: void testRetro() { int[] arr=[1,2,3,4,5]; auto retArr=retro(arr); for(int i=0;i<retArr;i++) writef("%d ",retArr[i]); } void main() { testRetro; } output: 0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here? static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - n + 1]; } Per my understanding,the above will return the nth element of _input in reverse order. I try to count my fingers with length-n+1 but cannot get a correct result.Looks like a bug. The fix is easy: static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - n - 1]; } or static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - (n + 1)]; }3. About isInfinte: template isInfinite(Range) { static if (isInputRange!(Range) && is(char[1 + Range.empty])) enum bool isInfinite = !Range.empty; else enum bool isInfinite = false; } In the is expression is(char[1+ Range.empty]),below is my understanding; Step 1: is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1]) Step 2: But what does this help with determining whether Range is infinite or not?say char[1+0],char[1+1]? Any help would be much much appreciated. Regards, Sam
Jun 17 2009