Ranges and backward iteration
Timon Gehr
timon.gehr at gmx.ch
Sun Jul 29 16:42:08 PDT 2012
On 07/30/2012 01:26 AM, Andrew wrote:
> I have a use case where I would like to be able to pass both a
> forward and backward iteration of an array to a function:
>
> void foo(InputR, OutputR)(InputR i, OutputR j)
> if (isInputRange!InputR && isOutputRange!(OutputR, InputR))
> {
> ...
> }
>
> main()
> {
> //forward:
> foo(a[1..n], b[1..n]);
> //backward:
> foo(retro(a[n..$]), retro(b[n..$]));
>
> //do stuff with b as it has been constructed now...
> }
>
> This doesn't work
Works for me.
This is how I test:
void foo(InputR, OutputR)(InputR i, OutputR j) if (isInputRange!InputR
&& isOutputRange!(OutputR, InputR)){
j.put(i);
}
void main() {
int[] a=[2,0,0,3], b=[1,2,3,4];
int n=2;
foo(a[0..n], b[0..n]);
assert(b==[2,0,3,4]);
foo(retro(a[n..$]), retro(b[n..$]));
assert(b==[2,0,0,3]);
}
> (retro doesn't appear to return a Range, but
> rather an object of type "Result", which I don't understand)
'Result' implements the range interface and is a local struct of the
'retro' function.
>, but the intent should be clear.
>
> How do I do this? What am I missing? There doesn't seem to be a
> "backward iterator" equivalent in std.range.
retro should do the job.
More information about the Digitalmars-d-learn
mailing list