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.

Function NameDescription
adjoinJoins a couple of functions into one that executes the original functions independently and returns a tuple with all the results.
compose, pipeJoin a couple of functions into one that executes the original functions one after the other, using one function's result for the next function's argument.
forwardForwards function arguments while saving ref-ness.
lessThan, greaterThan, equalToReady-made predicate functions to compare two values.
memoizeCreates a function that caches its result for fast re-evaluation.
notCreates a function that negates another.
partialCreates a function that binds the first argument of a given function to a given value.
curryConverts a multi-argument function into a series of single-argument functions. f(x, y) == curry(f)(x)(y)
reverseArgsPredicate that reverses the order of its arguments.
toDelegateConverts a callable to a delegate.
unaryFun, binaryFunCreate a unary or binary function from a string. Most often used when defining algorithms on ranges.

Members

Aliases

equalTo
alias equalTo = safeOp!"=="

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

greaterThan
alias greaterThan = safeOp!">"

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

lessThan
alias lessThan = safeOp!"<"

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

pipe
alias pipe(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.

Functions

curry
auto curry(T t)

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)

toDelegate
auto toDelegate(F fp)

Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.

Templates

adjoin
template adjoin(F...)

Takes multiple functions and adjoins them together.

binaryFun
template binaryFun(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.

compose
template compose(fun...)

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

curry
template curry(alias F)

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)

forward
template forward(args...)

Forwards function arguments while keeping out, ref, and lazy on the parameters.

memoize
template memoize(alias fun)
template memoize(alias fun, uint maxSize)

* 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.

not
template not(alias pred)

Negates predicate pred.

partial
template partial(alias fun, alias arg)

Partially applies fun by tying its first argument to arg.

reverseArgs
template reverseArgs(alias pred)

N-ary predicate that reverses the order of arguments, e.g., given pred(a, b, c), returns pred(c, b, a).

unaryFun
template unaryFun(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.

Meta

Suggestion Box / Bug Report