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.
Copyright
Types 5
Predicate that returns a < b. Correctly compares signed and unsigned integers, ie. -1 < 2U.
Predicate that returns a > b. Correctly compares signed and unsigned integers, ie. 2U > -1.
Predicate that returns a == b. Correctly compares signed and unsigned integers, ie. !(-1 == ~0U).
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
fun | the call-able(s) or string(s) to compose into one function |
Returns
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
F castToF(THIS)(THIS x) @trustedGeneratingPolicy()Functions 7
auto 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
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
fun | a string or a callable |
parmName | the name of the parameter if fun is a string. Defaults to "a". |
Returns
fun is a string, a new single parameter function
If fun is not a string, an alias to fun.
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
fun | a string or a callable |
parm1Name | the name of the first parameter if fun is a string. Defaults to "a". |
parm2Name | the name of the second parameter if fun is a string. Defaults to "b". |
Returns
fun is not a string, binaryFun aliases itself away to
fun.
if (S == "<" || S == ">" || S == "<=" || S == ">=" || S == "==" || S == "!=")N-ary predicate that reverses the order of arguments, e.g., given
pred(a, b, c), returns pred(c, b, a).
Parameters
pred | A callable |
Returns
pred after reversing the given parametersNegates predicate pred.
Parameters
pred | A string or a callable |
Returns
pred and returns the logical negation of its
return value.
applies
fun by tying its first argument to arg.Parameters
fun | A callable |
arg | The first argument to apply to fun |
Returns
fun with arg plus the passed parameters.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
F | a function taking at least one argument |
t | a callable object whose opCall takes at least 1 object |
Returns
if (F.length >= 1)Takes multiple functions and adjoins them together.
Parameters
F | the call-able(s) to adjoin |
Returns
Tuple. Each of the
elements of the tuple will be the return values of F.
Note
(F.length == 1), adjoin simply aliases to the single passed function (F[0]).
if (fun.length > 0)Composes passed-in functions fun[0], fun[1], ....
Parameters
fun | the call-able(s) or string(s) to compose into one function |
Returns
f(x) that in turn returns fun[0](fun[1](...(x)))....See Also
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
fun | the call-able to memozie |
maxSize | The maximum size of the GC buffer to hold the return values |
Returns
fun and caches its return values.Note
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.
ditto
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
fun | Callable that the struct's fields will be passed to. |
Returns
fields to fun when called.
Parameters
args | The struct or tuple whose fields will be used as arguments. |
Returns
fun(args.tupleof)