Created
September 25, 2025 23:33
-
-
Save crazymonkyyy/bb6c52dd08ffb55b8e38b3f66896f459 to your computer and use it in GitHub Desktop.
doesnt work yet
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
#!opend -mixin=mix -unittest -main -run app.d | |
import std; | |
auto toiter(R)(/*ref*/R r) if (isInputRange!R){ | |
return (int delegate(ref ElementType!R) dg){ | |
int ret; | |
while( ! r.empty){ | |
ret=dg(r.front); | |
r.popFront; | |
if(ret){return ret;} | |
} | |
return ret; | |
}; | |
} | |
auto toiter(T)(ref T t) if ( ! isInputRange!T){ | |
return (int delegate(ref T) dg){ | |
return dg(t); | |
}; | |
} | |
template myelementtype(T){ | |
static if(istuple!T){ | |
static if(isInputRange!(T.type!0)){ | |
alias myelementtype=ElementType!(T.type!0); | |
} else { | |
alias myelementtype=T.type!0; | |
} | |
} else { | |
static if(isInputRange!T){ | |
alias myelementtype=ElementType!T; | |
} else { | |
alias myelementtype=T; | |
}}} | |
unittest{ | |
//myelementtype!int.stringof.writeln; | |
//myelementtype!(int[]).stringof.writeln; | |
//myelementtype!(Tuple!(int[],int)).stringof.writeln; | |
//myelementtype!(Tuple!(int,int[])).stringof.writeln; | |
//myelementtype!(Tuple!(int,int[])).stringof.writeln; | |
} | |
string ndfieldheader(string order,T...)(){ | |
string output="(int delegate("; | |
string[T.length] types; | |
static foreach(I,c;order[0..T.length]){ | |
types[getndindex(c)]=/*"ref*/"myelementtype!(T["~I.to!string~"])"; | |
} | |
foreach(int i,s;types){ | |
output~="ref "; | |
output~=s; | |
output~=" "; | |
output~=getndchar(i); | |
output~=","; | |
} | |
output~="ref typeof(F("; | |
foreach(int i,s;types){ | |
output~=s; | |
output~=".init,"; | |
} | |
output~="))"; | |
output~=") dg)"; | |
return output; | |
} | |
string ndfieldloop(int I,string order,T...)(){ | |
//static if(I>=T.length||I>=order.length){return "";} | |
//else{ | |
string output; | |
if(I==0){output~="int ret;\n";} | |
static if(istuple!(T[I])){ | |
static assert(0); | |
} else { | |
output~="foreach("; | |
output~=order[I]; | |
output~=";args["; | |
output~=getndindex(order[I]).to!string; | |
output~="].toiter){\n"; | |
static if(I+1<order.length&&I+1<T.length){ | |
output~=ndfieldloop!(I+1,order,T); | |
} else { | |
output~="ret=dg("; | |
foreach(c;defualtndcharorder[0..I+1]){ | |
output~=c; | |
output~=','; | |
} | |
output~="F("; | |
foreach(c;defualtndcharorder[0..I+1]){ | |
output~=c; | |
output~=','; | |
} | |
output~="));\n"; | |
output~="if(ret>1){return ret;}\n"; | |
output~="if(ret){break;}\n"; | |
} | |
output~="}\n"; | |
} | |
if(I==0){output~="return ret;";} | |
return output; | |
} | |
unittest{ | |
//ndfieldheader!("yxz",int,int[],string)().writeln; | |
//ndfieldloop!(0,"yxz",int,int[],string)().writeln; | |
} | |
auto ndfield(alias F,string order=defualtndcharorder,T...)(T args){ | |
return mixin(ndfieldheader!(order,T)~"{"~ndfieldloop!(0,order,T)~"}"); | |
} | |
unittest{ | |
string[] foo=["foo","bar","baz"]; | |
foreach(x,y,c;ndfield!((a,b)=>foo[a][b],"xy")(iota(3),iota(3))){ | |
c.writeln; | |
} | |
"---".writeln; | |
string[][][] bar=[[["bar","foo"],["fiz","buz"]],[["NO"]]]; | |
foreach(x,y,z,w,c;ndfield!((a,b,c,d)=>bar[a][b][c][d],"zwxy")(iota(2),iota(3),0,0)){ | |
c.writeln; | |
} | |
} | |
//--- | |
char getndchar(int i){ | |
if(i<3){return cast(char)('x'+i);} | |
assert(i<26); | |
return cast(char)('w'-(i-3)); | |
} | |
enum string defualtndcharorder=iota(26).map!getndchar.to!string; | |
unittest{ | |
defualtndcharorder.writeln; | |
} | |
int getndindex(char c){ | |
if(c>='x'&&c<='z'){return c-'x';} | |
assert(c>='a'&&c<='w'); | |
return 3+('w'-c); | |
} | |
auto ASCIIMap(alias F)(string r){ | |
struct Map{ | |
string r; | |
auto front()=>F(r[0]); | |
void popFront(){r=r[1..$];} | |
bool empty()=>r.length==0; | |
} | |
return Map(r.dup); | |
} | |
unittest{ | |
//defualtndcharorder.ASCIIMap!getndindex.writeln; | |
} | |
struct Tuple(T...){ | |
enum istuple=true; | |
alias type(int I)=T[I]; | |
T expand; alias expand this; | |
} | |
unittest{ | |
Tuple!(int,string).type!0.stringof.writeln; | |
} | |
auto tuple(T...)(T args){ | |
return Tuple!T(args); | |
} | |
unittest{ | |
auto foo=tuple(1,"hi"); | |
assert(foo[0]==1); | |
assert(foo[1]=="hi"); | |
auto bar=tuple(); | |
} | |
enum istuple(T)=is(typeof(T.istuple)); | |
unittest{ | |
assert(istuple!(typeof(tuple(1,2)))==true); | |
assert(istuple!int==false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment