Skip to content

Instantly share code, notes, and snippets.

@crazymonkyyy
Created February 28, 2023 23:20
Show Gist options
  • Save crazymonkyyy/538c0717a6ff695b023ad82a3b58da56 to your computer and use it in GitHub Desktop.
Save crazymonkyyy/538c0717a6ff695b023ad82a3b58da56 to your computer and use it in GitHub Desktop.
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