www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Grouping variadic parameter tuples on offset and stride

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I'm currently developing a new lazy variadic generic 
range/algorithm substitute() at

https://github.com/nordlow/justd/blob/master/substitution.d#L261

I plan to propose for Phobos.

It is meant to be used as

     assert(`do_it`.substitute(`_`, ` `,
                               `d`, `g`,
                               `i`, `t`,
                               `t`, `o`)
                   .equal(`go to`));

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?
Jan 21 2016
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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.
Jan 21 2016
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 21 January 2016 at 22:35:09 UTC, H. S. Teoh wrote:
 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 .. $]));
 	}
Made it work. Thx
Jan 22 2016