naryFun

Aliases itself to a set of functions.

Transforms strings representing an expression into a binary function. The strings must use symbol names a, b, ..., z as the parameters. If functions[i] is not a string, naryFun aliases itself away to functions[i].

  1. auto ref naryFun(Args args)
    template naryFun(functions...)
    ref
    static if(is(typeof(fun) : string))
    naryFun
    (
    Args...
    )
    (
    auto ref Args args
    )
    if (
    args.length <= 26
    )
    if (
    functions.length >= 1
    )
  2. alias naryFun = fun.opCall
  3. alias naryFun = fun

Members

Aliases

naryFun
alias naryFun = fun
Undocumented in source.
naryFun
alias naryFun = fun.opCall
Undocumented in source.

Functions

naryFun
auto ref naryFun(Args args)

Specialization for string lambdas

Examples

// Strings are compiled into functions:
alias isEven = naryFun!("(a & 1) == 0");
assert(isEven(2) && !isEven(1));
alias less = naryFun!("a < b");
assert(less(1, 2) && !less(2, 1));
alias greater = naryFun!("a > b");
assert(!greater("1", "2") && greater("2", "1"));

naryFun accepts up to 26 arguments.

assert(naryFun!("a * b + c")(2, 3, 4) == 10);

naryFun can return by reference.

int a;
assert(&naryFun!("a")(a) == &a);

args parameter tuple

assert(naryFun!("args[0] + args[1]")(2, 3) == 5);

Multiple functions

alias fun = naryFun!(
    (uint a) => a,
    (ulong a) => a * 2,
    a => a * 3,
);

int a = 10;
long b = 10;
float c = 10;

assert(fun(a) == 10);
assert(fun(b) == 20);
assert(fun(c) == 30);

Meta