Created
February 28, 2023 23:20
-
-
Save crazymonkyyy/538c0717a6ff695b023ad82a3b58da56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std; | |
struct verifiedindex{ | |
int i; | |
} | |
auto enumeratedfilter(alias F,R)(R r){ | |
struct range{ | |
R r; | |
R r_old; | |
auto front(){return tuple(verifiedindex(i),r.front);} | |
int i; | |
void popFront(){ | |
i++; | |
r.popFront; | |
while(!r.empty && !F(r.front)){ | |
i++; | |
r.popFront; | |
} | |
} | |
bool empty(){return r.empty;} | |
auto opIndex(verifiedindex i){ | |
return r_old[i.i]; | |
} | |
} | |
if(F(r.front)){ | |
return range(r,r); | |
} else { | |
auto temp=range(r,r); | |
temp.popFront; | |
return temp; | |
} | |
} | |
void main(){ | |
int[] foo=[0,1,2,3,4,5]; | |
verifiedindex[] bar; | |
auto r=foo.map!(a=>a).enumeratedfilter!(a=>a%2); | |
foreach(i,e;r){ | |
e.writeln; | |
bar~=i; | |
} | |
bar.writeln; | |
foreach(i;bar){ | |
r[i].writeln; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment