Grouping variadic parameter tuples on offset and stride

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 21 14:35:09 PST 2016


On Thu, Jan 21, 2016 at 10:24:55PM +0000, Nordlöw via Digitalmars-d-learn wrote:
[...]
> I'm currently only lacking one thing ...namely a way to group the
> parameters of the call to a variadic function based on their offset
> and stride. That is if have
> 
> haystack.substitute(x0, y0,
>                     x1, y1,
>                     x2, y2, ...);
> 
> How do I extract x0,x1,x2 into a one parameter tuple and y0,y1,y2 into
> another?

Try this:

	import std.meta;
	
	template Stride(size_t stride, size_t offset, Args...)
	    if (stride > 0)
	{
	    static if (offset >= Args.length)
	        alias Stride = AliasSeq!();
	    else static if (stride >= Args.length)
	        alias Stride = AliasSeq!(Args[offset]);
	    else
	        alias Stride = AliasSeq!(Args[offset],
	                                 Stride!(stride, offset, Args[stride .. $]));
	}
	
	alias MyList = AliasSeq!("a", "b", "c", "d", "e", "f", "g", "h", "i");
	
	pragma(msg, Stride!(1, 0, MyList));
	pragma(msg, Stride!(2, 0, MyList));
	pragma(msg, Stride!(2, 1, MyList));
	pragma(msg, Stride!(3, 0, MyList));
	pragma(msg, Stride!(3, 1, MyList));
	pragma(msg, Stride!(3, 2, MyList));

Compiler output:

	tuple("a", "b", "c", "d", "e", "f", "g", "h", "i")
	tuple("a", "c", "e", "g", "i")
	tuple("b", "d", "f", "h")
	tuple("a", "d", "g")
	tuple("b", "e", "h")
	tuple("c", "f", "i")


T

-- 
This sentence is false.


More information about the Digitalmars-d-learn mailing list