General purpose function objects.
Variables | |
constexpr auto | boost::hana::always |
Return a constant function returning x regardless of the argument(s) it is invoked with. More... | |
constexpr auto | boost::hana::apply |
Invokes a Callable with the given arguments. More... | |
template<std::size_t n> | |
constexpr auto | boost::hana::arg |
Return the n th passed argument. More... | |
constexpr auto | boost::hana::capture |
Create a function capturing the given variables. More... | |
constexpr auto | boost::hana::compose |
Return the composition of two functions or more. More... | |
template<std::size_t n> | |
constexpr auto | boost::hana::curry |
Curry a function up to the given number of arguments. More... | |
boost::hana::mathtt | |
Invoke a function with the results of invoking other functions on its arguments. More... | |
constexpr auto | boost::hana::fix |
Return a function computing the fixed point of a function. More... | |
constexpr auto | boost::hana::flip |
Invoke a function with its two first arguments reversed. More... | |
constexpr auto | boost::hana::id |
The identity function – returns its argument unchanged. More... | |
constexpr auto | boost::hana::infix |
Return an equivalent function that can also be applied in infix notation. More... | |
constexpr auto | boost::hana::lockstep |
Invoke a function with the result of invoking other functions on its arguments, in lockstep. More... | |
constexpr auto | boost::hana::on |
Invoke a function with the result of invoking another function on each argument. More... | |
constexpr auto | boost::hana::overload |
Pick one of several functions to call based on overload resolution. More... | |
constexpr auto | boost::hana::overload_linearly |
Call the first function that produces a valid call expression. More... | |
constexpr auto | boost::hana::partial |
Partially apply a function to some arguments. More... | |
constexpr unspecified | boost::hana::_ {} |
Create simple functions representing C++ operators inline. More... | |
constexpr auto | boost::hana::reverse_partial |
Partially apply a function to some arguments. More... | |
|
constexpr |
#include <boost/hana/functional/always.hpp>
Return a constant function returning x
regardless of the argument(s) it is invoked with.
Specifically, always(x)
is a function such that
for any y...
. A copy of x
is made and it is owned by the always(x)
function. When always(x)
is called, it will return a reference to the x
it owns. This reference is valid as long as always(x)
is in scope.
|
constexpr |
#include <boost/hana/functional/apply.hpp>
Invokes a Callable with the given arguments.
This is equivalent to std::invoke that will be added in C++17. However, apply
is a function object instead of a function, which makes it possible to pass it to higher-order algorithms.
f | A Callable to be invoked with the given arguments. |
x... | The arguments to call f with. The number of x... must match the arity of f . |
|
constexpr |
#include <boost/hana/functional/arg.hpp>
Return the n
th passed argument.
Specifically, arg<n>(x1, ..., xn, ..., xm)
is equivalent to xn
. Note that indexing starts at 1, so arg<1>
returns the 1st argument, arg<2>
the 2nd and so on. Using arg<0>
is an error. Passing less than n
arguments to arg<n>
is also an error.
n | An unsigned integer representing the argument to return. n must be positive (meaning nonzero). |
x1,...,xm | A variadic pack of arguments from which the n th one is returned. |
We could have chosen arg
to be used like arg(n)(x...)
instead of arg<n>(x...)
. Provided all the arguments were of the same type, it would then be possible for n
to only be known at runtime. However, we would then lose the ability to assert the in-boundedness of n
statically.
I claim that the only interesting use case is with a compile-time n
, which means that the usage would become arg(int_<n>)(x...)
, which is more cumbersome to write than arg<n>(x...)
. This is open for discussion.
|
constexpr |
#include <boost/hana/functional/capture.hpp>
Create a function capturing the given variables.
Given 0 or more variables, capture
creates a closure that can be used to partially apply a function. This is very similar to partial
, except that capture
allows the partially applied function to be specified later. Specifically, capture(vars...)
is a function object taking a function f
and returning f
partially applied to vars...
. In other words,
f
must match the total number of arguments passed to it, i.e. sizeof...(vars) + sizeof...(args)
.
|
constexpr |
#include <boost/hana/functional/compose.hpp>
Return the composition of two functions or more.
compose
is defined inductively. When given more than two functions, compose(f, g, h...)
is equivalent to compose(f, compose(g, h...))
. When given two functions, compose(f, g)
is a function such that
If you need composition of the form f(g(x, y...))
, use demux
instead.
compose
is an associative operation; compose(f, compose(g, h))
is equivalent to compose(compose(f, g), h)
.
|
constexpr |
#include <boost/hana/functional/curry.hpp>
Curry a function up to the given number of arguments.
Currying is a technique in which we consider a function taking multiple arguments (or, equivalently, a tuple of arguments), and turn it into a function which takes a single argument and returns a function to handle the remaining arguments. To help visualize, let's denote the type of a function f
which takes arguments of types X1, ..., Xn
and returns a R
as
Then, currying is the process of taking f
and turning it into an equivalent function (call it g
) of type
This gives us the following equivalence, where x1
, ..., xn
are objects of type X1
, ..., Xn
respectively:
Currying can be useful in several situations, especially when working with higher-order functions.
This curry
utility is an implementation of currying in C++. Specifically, curry<n>(f)
is a function such that
Note that the n
has to be specified explicitly because the existence of functions with variadic arguments in C++ make it impossible to know when currying should stop.
Unlike usual currying, this implementation also allows a curried function to be called with several arguments at a time. Hence, the following always holds
Of course, this requires k
to be less than or equal to n
; failure to satisfy this will trigger a static assertion. This syntax is supported because it makes curried functions usable where normal functions are expected.
Another "extension" is that curry<0>(f)
is supported: curry<0>(f)
is a nullary function; whereas the classical definition for currying seems to leave this case undefined, as nullary functions don't make much sense in purely functional languages.
boost::hana::mathtt |
#include <boost/hana/functional/demux.hpp>
Invoke a function with the results of invoking other functions on its arguments.
Removes all consecutive duplicate elements from a Sequence.
Dual operation to fold_right
for sequences.
Dual operation to fold_left
for sequences.
Map a function over a Functor
.
Returns a Product
containing the longest prefix of a sequence satisfying a predicate, and the rest of the sequence.
Sort a sequence, optionally based on a custom predicate
.
Equivalent to reverse_fold
in Boost.Fusion and Boost.MPL.
Replace all the elements of a structure satisfying a predicate
with a fixed value.
Replace all the elements of a structure that compare equal to some value
with some new fixed value.
Partition a sequence based on a predicate
.
Monadic right-fold of a structure with a binary operation and an optional initial reduction state.
Monadic left-fold of a structure with a binary operation and an optional initial reduction state.
Composition of monadic functions.
Return the least element of a non-empty structure with respect to a predicate
, by default less
.
Return the greatest element of a non-empty structure with respect to a predicate
, by default less
.
Short-circuiting lexicographical comparison of two Iterable
s with an optional custom predicate, by default hana::less
.
Returns a hana::type
representing the compile-time hash of an object.
Group adjacent elements of a sequence that all respect a binary predicate, by default equality.
Right-fold of a structure using a binary operation and an optional initial reduction state.
Left-fold of a structure using a binary operation and an optional initial reduction state.
Collapse two levels of monadic structure into a single level.
Replace all the elements of a structure with a fixed value.
Extract a value in a given comonadic context.
Comonadic application of a function to a comonadic value.
Add an extra layer of comonadic context to a comonadic value.
Feed a monadic value into a monadic computation.
Computes the cartesian product of a sequence of sequences.
Apply a function on all the elements of a structure satisfying a predicate.
Apply a function on all the elements of a structure that compare equal to some value.
Applies another function n
times to its argument.
Specifically, demux(f)(g...)
is a function such that
Each g
is called with all the arguments, and then f
is called with the result of each g
. Hence, the arity of f
must match the number of g
s.
This is called demux
because of a vague similarity between this device and a demultiplexer in signal processing. demux
takes what can be seen as a continuation (f
), a bunch of functions to split a signal (g...
) and zero or more arguments representing the signal (x...
). Then, it calls the continuation with the result of splitting the signal with whatever functions where given.
demux
is associative. In other words (and noting demux(f, g) = demux(f)(g)
to ease the notation), it is true that demux(demux(f, g), h) == demux(f, demux(g, h))
.The signature of demux
is
Given a function f
and an argument x
, iterate<n>(f, x)
returns the result of applying f
n
times to its argument. In other words,
If n == 0
, iterate<n>(f, x)
returns the x
argument unchanged and f
is never applied. It is important to note that the function passed to iterate<n>
must be a unary function. Indeed, since f
will be called with the result of the previous f
application, it may only take a single argument.
In addition to what's documented above, iterate
can also be partially applied to the function argument out-of-the-box. In other words, iterate<n>(f)
is a function object applying f
n
times to the argument it is called with, which means that
This is provided for convenience, and it turns out to be especially useful in conjunction with higher-order algorithms.
Given a function \( f : T \to T \) and x
and argument of data type T
, the signature is
Given F
a Functor and U
a type that can be compared with T
's, the signature is
Given a Functor, a predicate pred
and a function f
, adjust_if
will adjust the elements of the Functor that satisfy the predicate with the function f
. In other words, adjust_if
will return a new Functor equal to the original one, except that the elements satisfying the predicate will be transformed with the given function. Elements for which the predicate is not satisfied are left untouched, and they are kept as-is in the resulting Functor.
Given a Functor
F
and a Logical
Bool
, the signature is
Given a sequence of sequences, cartesian_product
returns a new sequence of sequences containing the cartesian product of the original sequences. For this method to finish, a finite number of finite sequences must be provided.
Given a Sequence
S(T)
, the signature is
Given a monadic value and a monadic function, chain
feeds the monadic value into the function, thus performing some Monad-specific effects, and returns the result. An implementation of chain
must satisfy
For a monad M
, given a monadic value of type M(A)
and a monadic function \( f : A \to M(B) \), chain
has the signature
Given a value already in a comonadic context, duplicate
wraps this value with an additional layer of comonadic context. This can be seen as the dual operation to flatten
from the Monad concept.
Given a Comonad W
, the signature is
Given a comonadic value and a function accepting a comonadic input, extend
returns the result of applying the function to that input inside the comonadic context.
Given a Comonad W
and a function of type \( W(T) \to U \), the signature is
Given a value inside a comonadic context, extract it from that context, performing whatever effects are mandated by that context. This can be seen as the dual operation to the lift
method of the Applicative concept.
Given a Comonad W
, the signature is
Given F
a Functor, the signature is
Given a monadic value wrapped into two levels of monad, flatten
removes one such level. An implementation of flatten
must satisfy
For Sequence
s, this simply takes a Sequence
of Sequence
s, and returns a (non-recursively) flattened Sequence
.
For a Monad
M
, the signature of flatten
is
fold_left
is a left-associative fold using a binary operation. Given a structure containing x1, ..., xn
, a function f
and an optional initial state, fold_left
applies f
as follows
When the structure is empty, two things may arise. If an initial state was provided, it is returned as-is. Otherwise, if the no-state version of the function was used, an error is triggered. When the stucture contains a single element and the no-state version of the function was used, that single element is returned as is.
Given a Foldable
F
and an optional initial state of tag S
, the signatures for fold_left
are
fold_right
is a right-associative fold using a binary operation. Given a structure containing x1, ..., xn
, a function f
and an optional initial state, fold_right
applies f
as follows
fold_left
.When the structure is empty, two things may arise. If an initial state was provided, it is returned as-is. Otherwise, if the no-state version of the function was used, an error is triggered. When the stucture contains a single element and the no-state version of the function was used, that single element is returned as is.
Given a Foldable
F
and an optional initial state of tag S
, the signatures for fold_right
are
Given a finite Sequence and an optional predicate (by default equal
), group
returns a sequence of subsequences representing groups of adjacent elements that are "equal" with respect to the predicate. In other words, the groups are such that the predicate is satisfied when it is applied to any two adjacent elements in that group. The sequence returned by group
is such that the concatenation of its elements is equal to the original sequence, which is equivalent to saying that the order of the elements is not changed.
If no predicate is provided, adjacent elements in the sequence must all be compile-time Comparable
.
Given a Sequence s
with tag S(T)
, an IntegralConstant
Bool
holding a value of type bool
, and a predicate \( pred : T \times T \to Bool \), group
has the following signatures. For the variant with a provided predicate,
Given an arbitrary object x
, hana::hash
returns a hana::type
representing the hash of x
. In normal programming, hashes are usually numerical values that can be used e.g. as indices in an array as part of the implementation of a hash table. In the context of metaprogramming, we are interested in type-level hashes instead. Thus, hana::hash
must return a hana::type
object instead of an integer. This hana::type
must somehow summarize the object being hashed, but that summary may of course lose some information.
In order for the hash
function to be defined properly, it must be the case that whenever x
is equal to y
, then hash(x)
is equal to hash(y)
. This ensures that hana::hash
is a function in the mathematical sense of the term.
Given a Hashable
H
, the signature is
Given two Iterable
s xs
and ys
and a binary predicate pred
, lexicographical_compare
returns whether xs
is to be considered less than ys
in a lexicographical ordering. Specifically, let's denote the linearizations of xs
and ys
by [x1, x2, ...]
and [y1, y2, ...]
, respectively. If the first couple satisfying the predicate is of the form xi, yi
, lexicographical_compare
returns true. Otherwise, if the first couple to satisfy the predicate is of the form yi, xi
, lexicographical_compare
returns false. If no such couple can be found, lexicographical_compare
returns whether xs
has fewer elements than ys
.
Given two Iterable
s It1(T)
and It2(T)
and a predicate \( pred : T \times T \to Bool \) (where Bool
is some Logical
), lexicographical_compare
has the following signatures. For the variant with a provided predicate,
Given a non-empty structure and an optional binary predicate (less
by default), maximum
returns the greatest element of the structure, i.e. an element which is greater than or equal to every other element in the structure, according to the predicate.
If the structure contains heterogeneous objects, then the predicate must return a compile-time Logical
. If no predicate is provided, the elements in the structure must be Orderable, or compile-time Orderable if the structure is heterogeneous.
Given a Foldable F
, a Logical Bool
and a predicate \( \mathtt{pred} : T \times T \to Bool \), maximum
has the following signatures. For the variant with a provided predicate,
Given a non-empty structure and an optional binary predicate (less
by default), minimum
returns the least element of the structure, i.e. an element which is less than or equal to every other element in the structure, according to the predicate.
If the structure contains heterogeneous objects, then the predicate must return a compile-time Logical
. If no predicate is provided, the elements in the structure must be Orderable, or compile-time Orderable if the structure is heterogeneous.
Given a Foldable
F
, a Logical Bool
and a predicate \( \mathtt{pred} : T \times T \to Bool \), minimum
has the following signatures. For the variant with a provided predicate,
Given two monadic functions f
and g
, monadic_compose
returns a new function equivalent to the composition of f
with g
, except the result of g
is chain
ed into f
instead of simply passed to it, as with normal composition. monadic_compose
satisfies
compose
, monadic_compose
does not generalize nicely to arities higher than one. Hence, only unary functions may be used with monadic_compose
.Given a Monad
M
and two functions \( f : B \to M(C) \) and \( g : A \to M(B) \), the signature is
hana::fold_left
, and to have read the primer on monadic folds.monadic_fold_left<M>
is a left-associative monadic fold. Given a Foldable
with linearization [x1, ..., xn]
, a function f
and an optional initial state, monadic_fold_left<M>
applies f
as follows:
where f(-, xk)
denotes the partial application of f
to xk
, and |
is just the operator version of the monadic chain
.
When the structure is empty, one of two things may happen. If an initial state was provided, it is lifted to the given Monad and returned as-is. Otherwise, if the no-state version of the function was used, an error is triggered. When the stucture contains a single element and the no-state version of the function was used, that single element is lifted into the given Monad and returned as is.
Given a Monad
M
, a Foldable
F
, an initial state of tag S
, and a function \( f : S \times T \to M(S) \), the signatures of monadic_fold_left<M>
are
hana::fold_right
, and to have read the primer on monadic folds.monadic_fold_right<M>
is a right-associative monadic fold. Given a structure containing x1, ..., xn
, a function f
and an optional initial state, monadic_fold_right<M>
applies f
as follows
where f(xk, -)
denotes the partial application of f
to xk
, and |
is just the operator version of the monadic chain
. It is worth noting that the order in which the binary function should expect its arguments is reversed from monadic_fold_left<M>
.
When the structure is empty, one of two things may happen. If an initial state was provided, it is lifted to the given Monad and returned as-is. Otherwise, if the no-state version of the function was used, an error is triggered. When the stucture contains a single element and the no-state version of the function was used, that single element is lifted into the given Monad and returned as is.
Given a Monad
M
, a Foldable
F
, an initial state of tag S
, and a function \( f : T \times S \to M(S) \), the signatures of monadic_fold_right<M>
are
Specifically, returns an unspecified Product
whose first element is a sequence of the elements satisfying the predicate, and whose second element is a sequence of the elements that do not satisfy the predicate.
Given a Sequence S(T)
, an IntegralConstant
Bool
holding a value of type bool
, and a predicate \( T \to Bool \), partition
has the following signature:
Given F
a Functor and U
a type that can be compared with T
, the signature is
Given F
a Functor and Bool
a Logical, the signature is
This method has the same semantics as reverse_fold
in Boost.Fusion and Boost.MPL, with the extension that an initial state is not required. This method is equivalent to fold_right
, except that the accumulating function must take its arguments in reverse order, to match the order used in Fusion. In other words,
fold_right
. As an alias, reverse_fold
is not tag-dispatched on its own and fold_right
should be customized instead.Given a Foldable
F
and an optional initial state of tag S
, the signatures for reverse_fold
are
Given a Sequence and an optional predicate (by default less
), sort
returns a new sequence containing the same elements as the original, except they are ordered in such a way that if x
comes before y
in the sequence, then either predicate(x, y)
is true, or both predicate(x, y)
and predicate(y, x)
are false.
Also note that the sort is guaranteed to be stable. Hence, if x
comes before y
in the original sequence and both predicate(x, y)
and predicate(y, x)
are false, then x
will come before y
in the resulting sequence.
If no predicate is provided, the elements in the sequence must all be compile-time Orderable
.
Given a Sequence
S(T)
, a boolean IntegralConstant
Bool
and a binary predicate \( T \times T \to Bool \), sort
has the following signatures. For the variant with a provided predicate,
The first component of the returned Product
is a sequence for which all elements satisfy the given predicate. The second component of the returned Product
is a sequence containing the remainder of the argument. Both or either sequences may be empty, depending on the input argument. More specifically,
except that make_pair
may be an arbitrary Product
.
Given a Sequence
S(T)
, a Logical
Bool
and a predicate \( T \to Bool \), span
has the following signature:
Given F
a Functor, the signature is
While fold_left
reduces a structure to a summary value from the left, unfold_left
builds a sequence from a seed value and a function, starting from the left.
Given a Sequence
S
, an initial value state
of tag I
, an arbitrary Product P
and a function \( f : I \to P(I, T) \), unfold_left<S>
has the following signature:
While fold_right
reduces a structure to a summary value from the right, unfold_right
builds a sequence from a seed value and a function, starting from the right.
Given a Sequence
S
, an initial value state
of tag I
, an arbitrary Product P
and a function \( f : I \to P(T, I) \), unfold_right<S>
has the following signature:
Given a Sequence
and an optional binary predicate, unique
returns a new sequence containing only the first element of every subrange of the original sequence whose elements are all equal. In other words, it turns a sequence of the form [a, a, b, c, c, c, d, d, d, a]
into a sequence [a, b, c, d, a]
. The equality of two elements is determined by the provided predicate
, or by equal
if no predicate
is provided.
Given a Sequence
S(T)
, a Logical
Bool
and a binary predicate \( T \times T \to Bool \), unique
has the following signature:
|
constexpr |
#include <boost/hana/functional/fix.hpp>
Return a function computing the fixed point of a function.
fix
is an implementation of the Y-combinator, also called the fixed-point combinator. It encodes the idea of recursion, and in fact any recursive function can be written in terms of it.
Specifically, fix(f)
is a function such that
This definition allows f
to use its first argument as a continuation to call itself recursively. Indeed, if f
calls its first argument with y...
, it is equivalent to calling f(fix(f), y...)
per the above equation.
Most of the time, it is more convenient and efficient to define recursive functions without using a fixed-point combinator. However, there are some cases where fix
provides either more flexibility (e.g. the ability to change the callback inside f
) or makes it possible to write functions that couldn't be defined recursively otherwise.
f | A function called as f(self, x...) , where x... are the arguments in the fix(f)(x...) expression and self is fix(f) . |
|
constexpr |
#include <boost/hana/functional/flip.hpp>
Invoke a function with its two first arguments reversed.
Specifically, flip(f)
is a function such that
|
constexpr |
#include <boost/hana/functional/id.hpp>
The identity function – returns its argument unchanged.
|
constexpr |
#include <boost/hana/functional/infix.hpp>
Return an equivalent function that can also be applied in infix notation.
Specifically, infix(f)
is an object such that:
Hence, the returned function can still be applied using the usual function call syntax, but it also gains the ability to be applied in infix notation. The infix syntax allows a great deal of expressiveness, especially when used in combination with some higher order algorithms. Since operator^
is left-associative, x ^infix(f)^ y
is actually parsed as (x ^infix(f))^ y
. However, for flexibility, the order in which both arguments are applied in infix notation does not matter. Hence, it is always the case that
However, note that applying more than one argument in infix notation to the same side of the operator will result in a compile-time assertion:
Additionally, a function created with infix
may be partially applied in infix notation. Specifically,
^
operator was chosen because it is left-associative and has a low enough priority so that most expressions will render the expected behavior.f | The function which gains the ability to be applied in infix notation. The function must be at least binary; a compile-time error will be triggered otherwise. |
|
constexpr |
#include <boost/hana/functional/lockstep.hpp>
Invoke a function with the result of invoking other functions on its arguments, in lockstep.
Specifically, lockstep(f)(g1, ..., gN)
is a function such that
Since each g
is invoked on its corresponding argument in lockstep, the number of arguments must match the number of g
s.
|
constexpr |
#include <boost/hana/functional/on.hpp>
Invoke a function with the result of invoking another function on each argument.
Specifically, on(f, g)
is a function such that
For convenience, on
also supports infix application as provided by infix
.
on
is associative, i.e. on(f, on(g, h))
is equivalent to on(on(f, g), h)
.
|
constexpr |
#include <boost/hana/functional/overload.hpp>
Pick one of several functions to call based on overload resolution.
Specifically, overload(f1, f2, ..., fn)
is a function object such that
where fk
is the function of f1, ..., fn
that would be called if overload resolution was performed amongst that set of functions only. If more than one function fk
would be picked by overload resolution, then the call is ambiguous.
|
constexpr |
#include <boost/hana/functional/overload_linearly.hpp>
Call the first function that produces a valid call expression.
Given functions f1, ..., fn
, overload_linearly(f1, ..., fn)
is a new function that calls the first fk
producing a valid call expression with the given arguments. Specifically,
where fk
is the first function such that fk(args...)
is a valid expression.
|
constexpr |
#include <boost/hana/functional/partial.hpp>
Partially apply a function to some arguments.
Given a function f
and some arguments, partial
returns a new function corresponding to the partially applied function f
. This allows providing some arguments to a function and letting the rest of the arguments be provided later. Specifically, partial(f, x...)
is a function such that
f
must match the total number of arguments passed to it, i.e. sizeof...(x) + sizeof...(y)
.
|
constexpr |
#include <boost/hana/functional/placeholder.hpp>
Create simple functions representing C++ operators inline.
Specifically, _
is an object used as a placeholder to build function objects representing calls to C++ operators. It works by overloading the operators between _
and any object so that they return a function object which actually calls the corresponding operator on its argument(s). Hence, for any supported operator @
:
Operators may also be partially applied to one argument inline:
When invoked with more arguments than required, functions created with _
will discard the superfluous instead of triggering an error:
This makes functions created with _
easier to use in higher-order algorithms, which sometime provide more information than necessary to their callbacks.
+
, binary -
, /
, *
, %
, unary +
, unary -
~
, &
, |
, ^
, <<
, >>
==
, !=
, <
, <=
, >
, >=
||
, &&
, !
*
(dereference), []
(array subscript)()
(function call)More complex functionality like the ability to compose placeholders into larger function objects inline are not supported. This is on purpose; you should either use C++14 generic lambdas or a library like Boost.Phoenix if you need bigger guns. The goal here is to save you a couple of characters in simple situations.
|
constexpr |
#include <boost/hana/functional/reverse_partial.hpp>
Partially apply a function to some arguments.
Given a function f
and some arguments, reverse_partial
returns a new function corresponding to f
whose last arguments are partially applied. Specifically, reverse_partial(f, x...)
is a function such that
f
must match the total number of arguments passed to it, i.e. sizeof...(x) + sizeof...(y)
.