std.functional

Functions that manipulate other functions.

This module provides functions for compile time function composition. These functions are helpful when constructing predicates for the algorithms in

std.algorithm or std.range.

Types 5

aliaslessThan = safeOp!"<"

Predicate that returns a < b. Correctly compares signed and unsigned integers, ie. -1 < 2U.

aliasgreaterThan = safeOp!">"

Predicate that returns a > b. Correctly compares signed and unsigned integers, ie. 2U > -1.

aliasequalTo = safeOp!"=="

Predicate that returns a == b. Correctly compares signed and unsigned integers, ie. !(-1 == ~0U).

aliaspipe(fun...) = compose!(Reverse!(fun))

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.

Parameters

funthe call-able(s) or string(s) to compose into one function

Returns

A new function f(x) that in turn returns fun[$-1](...fun[1](fun[0](x)))....

Example:

---- // 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"); ----

See Also

private structDelegateFaker(F)
Methods
F castToF(THIS)(THIS x) @trusted
Nested Templates
GeneratingPolicy()

Functions 7

private fnuint _ctfeSkipOp(ref string op)
private fnuint _ctfeSkipInteger(ref string op)
private fnuint _ctfeSkipName(ref string op, string name)
private fnuint _ctfeMatchUnary(string fun, string name)
private fnuint _ctfeMatchBinary(string fun, string name1, string name2)
fnauto curry(T)(T t) if (isCallable!T && Parameters!T.length)ditto
fnauto toDelegate(F)(auto ref F fp) if (isCallable!(F))Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.

Templates 16

tmplneedOpCallAlias(alias fun)
tmplunaryFun(alias fun, string parmName = "a")

Transforms a string representing an expression into a unary function. The string must either use symbol name a as the parameter or provide the symbol via the parmName argument.

Parameters

funa string or a callable
parmNamethe name of the parameter if fun is a string. Defaults to "a".

Returns

If fun is a string, a new single parameter function

If fun is not a string, an alias to fun.

tmplbinaryFun(alias fun, string parm1Name = "a", string parm2Name = "b")

Transforms a string representing an expression into a binary function. The string must either use symbol names a and b as the parameters or provide the symbols via the parm1Name and parm2Name arguments.

Parameters

funa string or a callable
parm1Namethe name of the first parameter if fun is a string. Defaults to "a".
parm2Namethe name of the second parameter if fun is a string. Defaults to "b".

Returns

If fun is not a string, binaryFun aliases itself away to

fun.

tmplsafeOp(string S) if (S == "<" || S == ">" || S == "<=" || S == ">=" || S == "==" || S == "!=")
Functions
bool unsafeOp(ElementType1, ElementType2)(ElementType1 a, ElementType2 b) if (isIntegral!ElementType1 && isIntegral!ElementType2)
bool safeOp(T0, T1)(auto ref T0 a, auto ref T1 b)
tmplreverseArgs(alias pred)

N-ary predicate that reverses the order of arguments, e.g., given

pred(a, b, c), returns pred(c, b, a).

Parameters

predA callable

Returns

A function which calls pred after reversing the given parameters
Functions
auto reverseArgs(Args...)(auto ref Args args) if (is(typeof(pred(Reverse!args))))
tmplnot(alias pred)

Negates predicate pred.

Parameters

predA string or a callable

Returns

A function which calls pred and returns the logical negation of its

return value.

Functions
auto not(T...)(auto ref T args)
tmplpartial(alias fun, alias arg)
Partially

applies fun by tying its first argument to arg.

Parameters

funA callable
argThe first argument to apply to fun

Returns

A new function which calls fun with arg plus the passed parameters.
tmplcurry(alias F) if (isCallable!F && Parameters!F.length)

Takes a function of (potentially) many arguments, and returns a function taking one argument and returns a callable taking the rest. f(x, y) == curry(f)(x)(y)

Parameters

Fa function taking at least one argument
ta callable object whose opCall takes at least 1 object

Returns

A single parameter callable object
Functions
auto curry()
Types
struct CurryImpl
tmplIota(size_t n)
tmpladjoin(F...) if (F.length >= 1)

Takes multiple functions and adjoins them together.

Parameters

Fthe call-able(s) to adjoin

Returns

A new function which returns a Tuple. Each of the

elements of the tuple will be the return values of F.

Note

In the special case where only a single function is provided

(F.length == 1), adjoin simply aliases to the single passed function (F[0]).

tmplcompose(fun...) if (fun.length > 0)

Composes passed-in functions fun[0], fun[1], ....

Parameters

funthe call-able(s) or string(s) to compose into one function

Returns

A new function f(x) that in turn returns fun[0](fun[1](...(x)))....

See Also

tmplgetOverloads(alias fun)
tmplmemoize(alias fun)
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.

Example: ---- double transmogrify(int a, string b) { ... expensive computation ... } alias fastTransmogrify = memoize!transmogrify; unittest { auto slow = transmogrify(2, "hello"); auto fast = fastTransmogrify(2, "hello"); assert(slow == fast); } ----

Parameters

funthe call-able to memozie
maxSizeThe maximum size of the GC buffer to hold the return values

Returns

A new function which calls fun and caches its return values.

Note

Technically the memoized function should be pure because memoize assumes it will

always return the same result for a given tuple of arguments. However, memoize does not enforce that because sometimes it is useful to memoize an impure function, too.

Functions
auto impl(Args...)(Args args) if (is(typeof(fun(args))))
tmplmemoize(alias fun, uint maxSize)

ditto

Functions
auto impl(Args...)(Args args) if (is(typeof(fun(args))))
tmplbuildDelegate(F)
Functions
auto buildDelegate(auto ref F fp)
tmplbind(alias fun)

Passes the fields of a struct as arguments to a function.

Can be used with a function literal to give temporary names to the fields of a struct or tuple.

Parameters

funCallable that the struct's fields will be passed to.

Returns

A function that accepts a single struct as an argument and passes its

fields to fun when called.

Functions
auto ref bind(T)(auto ref T args) if (is(T == struct))

Parameters

argsThe struct or tuple whose fields will be used as arguments.

Returns

fun(args.tupleof)