On 08/06/13 18:03, Dicebot wrote:
> On Tuesday, 6 August 2013 at 15:51:28 UTC, Jack Applegame wrote:
>> On Tuesday, 6 August 2013 at 14:44:18 UTC, bearophile wrote:
>>> Take a look at std.typetuple.staticMap
>> staticMap works with types, and I don't know how it can help.
>
> Not really. Anything that can be provided to template argument list can be
> stored in TypeTuple and thus processed by staticMap. Name is very confusing
> here.
The problem is more that staticMap requires a *template*, not a
function. Another one is that doing it like that (ie storing the
intermediate results in a tuple or struct) creates unnecessary
copies - which isn't really an issue for small PODs, because the
compiler optimizes most of the overhead away, but can have a
significant cost when handling types w/ nontrivial cpctors etc.
Anyway, there are several ways to do this in D. One example:
void foo(Args...)(Args args) {
mixin(evalExpMap!(q{ somefunc(%...); }, q{ process(%s) }, args));
// == `somefunc(process(args[0]), process(args[1]), etc);`
}
// or
void foo(Args...)(Args args) {
auto result = mixin(evalExpMap!(q{ somefunc(%...) }, q{ process(%s) },
args));
//...
}
// and the helper:
template evalExpMap(string C, string F, A...) {
enum evalExpMap = {
import std.array, std.conv;
string s, l;
static if (is(typeof(A))) alias B = typeof(A);
else alias B = A;
foreach (I, _; B) {
auto r = replace( replace(F, "%s", A[I].stringof),
"%d", to!string(I));
l ~= (I?", ":"") ~ r;
s ~= r ~ ";\n";
}
return replace(replace(C, "%...;", s), "%...", l);
}();
}
Not as simple as the C++ equivalent. But not that much more
complicated and *much* more powerful.
artur