Created
September 26, 2024 01:53
-
-
Save crazymonkyyy/81b4499c1a12a117ad9c1a1878ce2544 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; | |
alias seq=AliasSeq; | |
template isrange(alias R,string s){ | |
static if(__traits(compiles,mixin("isInputRange!(typeof(R."~s~"))"))){ | |
enum isrange=mixin("isInputRange!(typeof(R."~s~"))"); | |
} else { | |
enum isrange=false; | |
}} | |
template childranges(R){ | |
alias store=seq!(); | |
static foreach(s;FieldNameTuple!R){ | |
static if(isrange!(R,s)){ | |
//store=seq!(store,__traits(getMember,R,s)); | |
store=seq!(store,s); | |
} | |
} | |
//alias childranges=store; | |
enum string[] childranges=[store]; | |
} | |
//unittest{ | |
// auto foo=[1,2,3]; | |
// auto bar=foo.map!"a*2".filter!(a=>a!=4); | |
// childranges!(typeof(bar)).length.writeln; | |
//} | |
auto dig(string func,R)(R r){ | |
static if(__traits(compiles,mixin("r."~func))){ | |
return mixin("r."~func); | |
} else { | |
alias childs=childranges!R; | |
//pragma(msg,childs.stringof); | |
static assert(childs.length==1); | |
return mixin("r."~childs[0]~".dig!func"); | |
//return __traits(child,childs[0],r).dig!func; | |
} | |
} | |
//unittest{ | |
// auto foo=[1,2,3]; | |
// auto bar=foo.map!"a*2".filter!(a=>a!=4).map!"a*2"; | |
// dig!"length"(bar).writeln; | |
// bar.popFront; | |
// dig!"length"(bar).writeln; | |
//} | |
auto capisity(R)(R r)=>r.dig!"length"; | |
unittest{ | |
auto foo=[1,2,3,4,5]; | |
assert(foo.map!"a*2".filter!"false".map!"a*2".dig!"length"<=5); | |
assert(foo.map!"a*2".capisity==5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment