digitalmars.D.learn - workarounds for foreach limitations
- monkyyy (91/91) Jul 28 still want this:
still want this: https://forum.dlang.org/thread/lmvyzfatklcvrijcyhgk forum.dlang.org I dont think it will magicly appear in a few hours but maybe someone wants to write the templated opApply code some active code: ```d auto takewhilecmp(string op="<",R,T)(R r,ref T senti){ struct range{ R r; T* senti; auto front()=>r.front; void popFront()=>r.popFront; bool empty()=>mixin(" ! (r.front"~op~"*senti)") || r.empty; } return range(r,&senti); } unittest{ int i=100; foreach(e;iota(0,1000).takewhilecmp(i)){ i-=e; //e.writeln; } } ref T reintupitive(T,S)(ref S t)=>*cast(T*)&t; auto indexs(T)(ref T[] r){ struct slicesimple{ ulong length; T* data; } return iota(0,int.max).takewhilecmp!"<"(r.reintupitive!(slicesimple).length); } unittest{ int[] foo=[1,2,5,7]; foreach(i;foo.indexs){ if(foo[i]%2){ foo~=foo[i]*2; } if(i==3){ foo~=25; }} //foo.writeln; } void printarray(A)(A a){ foreach(i;a.indexs){ writeln(i,":",a[i]); }} auto indexs(T,S)(ref T[S] a)=>a.byKey; auto indexs(T,int N)(ref T[N] a)=>iota(0,N); //unittest{ // printarray([1,2,5,7]); // printarray([1:"foo",2:"bar"]); // int[5] foo=[1,2,3,4,5]; // printarray(foo); //} 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 iteratebyindex(A,R)(ref A a,R r){ 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,r); } unittest{ int[] foo=[1,2,5,7]; foreach(e;foo.iteratebyindex){ if(e%2){ foo~=e*2; } } //foo.writeln; } ``` I will want an foreach with access to an index generated by `iteratebyindex` after a split(which is one of the functions that breaks countUntil, also see filter and unicode handling)
Jul 28








monkyyy <crazymonkyyy gmail.com>