View source code Display the source code in std/functional.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

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.
composepipe 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.
forward Forwards function arguments while saving ref-ness.
lessThan, greaterThan, equalTo Ready-made predicate functions to compare two values.
memoize Creates a function that caches its result for fast re-evalation.
not Creates a function that negates another.
partial Creates a function that binds the first argument of a given function to a given value.
reverseArgs, binaryReverseArgs 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.

Functions

Name Description
memoize 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 Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.

Templates

Name Description
adjoin 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.
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. If fun is not a string, binaryFun aliases itself away to fun.
binaryReverseArgs Binary predicate that reverses the order of arguments, e.g., given pred(a, b), returns pred(b, a).
compose 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.
forward Forwards function arguments with saving ref-ness.
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. If fun is not a string, unaryFun aliases itself away to fun.

Aliases

Name Type Description
adjoin F[0] 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.
curry Deprecated alias for partial, kept for backwards compatibility
equalTo Predicate that returns a == b. Correctly compares signed and unsigned integers, ie. !(-1 == ~0U).
greaterThan Predicate that returns a > b. Correctly compares signed and unsigned integers, ie. 2U > -1.
lessThan 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.

Comments