Compiler errors and recursive nested functions
Dylan Knutson
tcdknutson at gmail.com
Wed Jun 12 15:49:30 PDT 2013
DPaste has stopped responding to requests at the time of writing,
so here's the code in case its still down by the time someone
reads this:
----
import std.algorithm : map;
import std.range : isInputRange, ElementType;
// Doesn't work
//template deepMap(alias pred, Range)
//if(isInputRange!Range)
//{
// static if(isInputRange!(ElementType!Range))
// {
// alias deepMap = map!(a => deepMap!(pred, typeof(a))(a));
// }
// else
// {
// alias deepMap = map!(pred);
// }
//}
// Neither does this, for the same reason
auto deepMap(alias pred, Range)(Range r)
if(isInputRange!Range)
{
static if(isInputRange!(ElementType!Range))
{
return r.map!( a => a.deepMap!(pred)() )();
}
else
{
return r.map!(pred)();
}
}
unittest {
import std.stdio;
auto a = [[1, 1], [1, 1]];
auto b = a.deepMap!(a => a + 1)();
writeln(b);
//assert(b == [[2, 2], [2, 2]]);
}
----
More information about the Digitalmars-d-learn
mailing list