Skip to content

Commit

Permalink
Merge pull request #2013 from monarchdodra/adjoinBuild
Browse files Browse the repository at this point in the history
improve adjoin
  • Loading branch information
andralex committed Mar 16, 2014
2 parents 32c72bb + f92dcf5 commit b2fde0a
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions std/functional.d
Expand Up @@ -353,32 +353,29 @@ Note: In the special case where where only a single function is provided
($(D F.length == 1)), adjoin simply aliases to the single passed function
($(D F[0])).
*/
template adjoin(F...) if (F.length)
template adjoin(F...) if (F.length == 1)
{
static if (F.length == 1)
{
alias adjoin = F[0];
}
else
alias adjoin = F[0];
}
/// ditto
template adjoin(F...) if (F.length > 1)
{
auto adjoin(V...)(auto ref V a)
{
auto adjoin(V...)(auto ref V a)
import std.typecons : tuple;
static if (F.length == 2)
{
import std.typecons : Tuple, tuple;
static if (F.length == 2)
{
return tuple(F[0](a), F[1](a));
}
else
{
import std.conv : emplaceRef;
alias Head = typeof(F[0](a));
Tuple!(Head, typeof(.adjoin!(F[1..$])(a)).Types) result = void;
foreach (i, Unused; result.Types)
{
emplaceRef(result[i], F[i](a));
}
return result;
}
return tuple(F[0](a), F[1](a));
}
else static if (F.length == 3)
{
return tuple(F[0](a), F[1](a), F[2](a));
}
else
{
import std.string : format;
import std.range : iota;
return mixin (q{tuple(%(F[%s](a)%|, %))}.format(iota(0, F.length)));
}
}
}
Expand Down Expand Up @@ -420,6 +417,25 @@ unittest
assert(x4 == 43);
}

unittest
{
import std.typetuple : staticMap;
import std.typecons : Tuple, tuple;
alias funs = staticMap!(unaryFun, "a", "a * 2", "a * 3", "a * a", "-a");
alias afun = adjoin!funs;
assert(afun(5) == tuple(5, 10, 15, 25, -5));

static class C{}
alias IC = immutable(C);
IC foo(){return typeof(return).init;}
Tuple!(IC, IC, IC, IC) ret1 = adjoin!(foo, foo, foo, foo)();

static struct S{int* p;}
alias IS = immutable(S);
IS bar(){return typeof(return).init;}
enum Tuple!(IS, IS, IS, IS) ret2 = adjoin!(bar, bar, bar, bar)();
}

// /*private*/ template NaryFun(string fun, string letter, V...)
// {
// static if (V.length == 0)
Expand Down

0 comments on commit b2fde0a

Please sign in to comment.