View source code
Display the source code in std/functional.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module 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 Name Description
adjoin Joins a couple of functions into one that executes the original functions independently and returns a tuple with all the results.
compose, pipe Join 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.
lessThan, greaterThan, equalTo Ready-made predicate functions to compare two values.
memoize Creates a function that caches its result for fast re-evaluation.
not Creates a function that negates another.
partial Creates a function that binds the first argument of a given function to a given value.
curry Converts a multi-argument function into a series of single-argument functions. f(x, y) == curry(f)(x)(y)
reverseArgs Predicate that reverses the order of its arguments.
toDelegate Converts a callable to a delegate.
unaryFun, binaryFun Create a unary or binary function from a string. Most often used when defining algorithms on ranges.
bind Passes the fields of a struct as arguments to a function.

Functions

NameDescription
curry() 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)
memoize(args) 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.
toDelegate(fp) Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.

Templates

NameDescription
binaryFun 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.
bind Passes the fields of a struct as arguments to a function.
not Negates predicate pred.
partial Partially applies fun by tying its first argument to arg.
reverseArgs N-ary predicate that reverses the order of arguments, e.g., given pred(a, b, c), returns pred(c, b, a).
unaryFun 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.

Aliases

NameTypeDescription
adjoin F[0] Takes multiple functions and adjoins them together.
compose unaryFun!(fun[0]) Composes passed-in functions fun[0], fun[1], ....
equalTo safeOp!"==" Predicate that returns a == b. Correctly compares signed and unsigned integers, ie. !(-1 == ~0U).
greaterThan safeOp!">" Predicate that returns a > b. Correctly compares signed and unsigned integers, ie. 2U > -1.
lessThan safeOp!"<" Predicate that returns a < b. Correctly compares signed and unsigned integers, ie. -1 < 2U.
pipe 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.

Authors

Andrei Alexandrescu

License

Boost License 1.0.