www.digitalmars.com

D Programming Language 2.0

Last update Wed Apr 23 23:20:09 2008

std.functional

Functions that manipulate other functions.

Author:
Andrei Alexandrescu

template unaryFun(string comp,bool byRef = false)
Transforms a string representing an expression into a unary function. The string must use symbol name a as the parameter.

Example:
alias unaryFun!("(a & 1) == 0") isEven;
assert(isEven(2) && !isEven(1));


template binaryFun(string comp)
Transforms a string representing an expression into a Boolean binary predicate. The string must use symbol names a and b as the compared elements.

Example:
alias binaryFun!("a < b") less;
assert(less(1, 2) && !less(2, 1));
alias binaryFun!("a > b") greater;
assert(!greater("1", "2") && greater("2", "1"));


template adjoin(F...)
Takes multiple functions and adjoins them together. The result is a std.typecons.Tuple with one element per passed-in function. Upon invocation, the returned tuple is the adjoined results of all functions.

Example:
static bool f1(int a) { return a != 0; }
static int f2(int a) { return a / 2; }
auto x = adjoin!(f1, f2)(5);
assert(is(typeof(x) == Tuple!(bool, int)));
assert(x._0 == true && x._1 == 2);


template compose(alias F1,alias F2)
template compose(alias F1,string F2)
template compose(string F1,alias F2)
template compose(string F1,string F2)
Composes two functions F1 and F2, returning a function F(x) that in turn returns F1(F2(x)).

Example:
auto x = 5;
auto y = compose!("!a", "a > 0")(x);
assert(!y);