opend -unittest -main -run foo.d
--- foo.d
import std;
auto indexs(T,int N)(ref T[N] a)=>iota(0,N);
auto iteratebyindex(A)(ref A a){
alias R=typeof(a.indexs);
struct range{
A* a;
R r;
auto ref front()=>(*a)[r.front];
auto ref index()=>r.front;
void popFront()()=>r.popFront;
bool empty()=>r.empty;
}
return range(&a,a.indexs);
}
auto map(alias F,R)(R r){
struct Map{
R r;
auto ref front()=>F(r.front);
auto ref index()=>r.index;
void popFront()()=>r.popFront;
bool empty()=>r.empty;
}
return Map(r);
}
unittest{
int[5] foo=[1,2,4,3,5];
foreach(i,e;foo.iteratebyindex.map!(a=>a*2)){
writeln(i,':',e);
}}
error: expected 1 argument
foreach of a range with 2 arguments should attempt to match i to r.index and e to r.front
foreach of a range with 2 arguments should attempt to match
itor.indexandetor.front