Three questions on std.range

Max Samukha outer at space.com
Wed Jun 17 13:00:36 PDT 2009


On Wed, 17 Jun 2009 00:04:51 -0400, Sam Hu <samhudotsamhu at 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?
>
>2.Continue to #1,In the source:
>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


More information about the Digitalmars-d-learn mailing list