Issue Turning Template into Variadic Template

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 31 10:44:57 PDT 2016


On Thu, Mar 31, 2016 at 07:43:38PM +0200, ag0aep6g via Digitalmars-d-learn wrote:
> On 30.03.2016 20:12, jmh530 wrote:
> >I wrote a version of cartesianProduct that will return the cartesian
> >product when the some of the types are not ranges. The original code
> >is below.
> >
> >My issue is that I can't figure out how to turn it into a variadic
> >template. The latest thing I tried is:
> >
> >auto mixedCartesianProduct(T...)(T x)
> >{
> >     import std.algorithm : cartesianProduct;
> >
> >     foreach(t; x)
> >             t = conditionalOnly(t);
> >
> >     return cartesianProduct(x);
> >}
> 
> auto mixedCartesianProduct(T...)(T x)
> {
>     import std.algorithm : cartesianProduct;
>     import std.meta: staticMap;
>     import std.traits : ReturnType;
> 
>     alias ConditionalOnly(T) = ReturnType!(conditionalOnly!T);
>     alias RangeTypes = staticMap!(ConditionalOnly, T);
> 
>     RangeTypes ranges;
>     foreach (i, t; x) ranges[i] = conditionalOnly(t);
> 
>     return cartesianProduct(ranges);
> }

+1, very nice. :-)  Much more concise and to-the-point than my attempt.
And how did I not remember we have staticMap in Phobos... :-P


T

-- 
Don't get stuck in a closet---wear yourself out.


More information about the Digitalmars-d-learn mailing list