Functions that manipulate other functions.
Transforms a string representing an expression into a unary function. The string must use symbol name a as the parameter.
alias unaryFun!("(a & 1) == 0") isEven; assert(isEven(2) && !isEven(1));
Transforms a string representing an expression into a Boolean binary predicate. The string must use symbol names a and b as the compared elements.
alias binaryFun!("a < b") less; assert(less(1, 2) && !less(2, 1)); alias binaryFun!("a > b") greater; assert(!greater("1", "2") && greater("2", "1"));
Binary predicate that reverses the order of arguments, e.g., given pred(a, b), returns pred(b, a).
Negates predicate pred.
string a = " Hello, world!"; assert(find!(not!isWhite)(a) == "Hello, world!");
Curries fun by tying its first argument to a particular value.
int fun(int a, int b) { return a + b; } alias curry!(fun, 5) fun5; assert(fun5(6) == 11);
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.
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);
Composes passed-in functions fun[0], fun[1], ... returning a function f(x) that in turn returns fun[0](fun[1](...(x))).... Each function can be a regular functions, a delegate, or a string.
// First split a string in whitespace-separated tokens and then // convert each token into an integer assert(compose!(map!(to!(int)), split)("1 2 3") == [1, 2, 3]);
Pipes functions in sequence. Offers the same functionality as compose, but with functions specified in reverse order. This may lead to more readable code in some situation because the order of execution is the same as lexical order.
// Read an entire text file, split the resulting string in // whitespace-separated tokens, and then convert each token into an // integer int[] a = pipe!(readText, split, map!(to!(int)))("file.txt");
Memoizes a function so as to avoid repeated computation. The memoization structure is a hash table keyed by a tuple of the function's arguments. There is a speed gain if the function is repeatedly called with the same arguments and is more expensive than a hash table lookup. For more information on memoization, refer to this book chapter.
double transmogrify(int a, string b) { ... expensive computation ... } alias memoize!transmogrify fastTransmogrify; unittest { auto slow = transmogrify(2, "hello"); auto fast = fastTransmogrify(2, "hello"); assert(slow == fast); }
ulong fib(ulong n) { alias memoize!fib mfib; return n < 2 ? 1 : mfib(n - 2) + mfib(n - 1); } ... assert(fib(10) == 89);
ulong fact(ulong n) { alias memoize!fact mfact; return n < 2 ? 1 : n * mfact(n - 1); } ... assert(fact(10) == 3628800);
ulong factImpl(ulong n) { return n < 2 ? 1 : n * mfact(n - 1); } alias memoize!factImpl fact; ... assert(fact(10) == 3628800);
// Memoize no more than 128 values of transmogrify alias memoize!(transmogrify, 128) fastTransmogrify;
Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.
void doStuff() { writeln("Hello, world."); } void runDelegate(void delegate() myDelegate) { myDelegate(); } auto delegateToPass = toDelegate(&doStuff); runDelegate(delegateToPass); // Calls doStuff, prints "Hello, world."