Boost.Hana  1.7.1
Your standard library for metaprogramming
Functional

Description

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

Variable Documentation

◆ always

constexpr auto boost::hana::always
constexpr

#include <boost/hana/functional/always.hpp>

Initial value:
= [](auto&& x) {
return [perfect-capture](auto const& ...y) -> decltype(auto) {
return forwarded(x);
};
}
constexpr auto capture
Create a function capturing the given variables.
Definition: capture.hpp:45

Return a constant function returning x regardless of the argument(s) it is invoked with.

Specifically, always(x) is a function such that

always(x)(y...) == x
constexpr auto always
Return a constant function returning x regardless of the argument(s) it is invoked with.
Definition: always.hpp:37

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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
static_assert(hana::always(1)() == 1, "");
static_assert(hana::always('2')(1, 2, 3) == '2', "");
int main() { }
Defines boost::hana::always.
Namespace containing everything in the library.
Definition: accessors.hpp:20

◆ apply

constexpr auto boost::hana::apply
constexpr

#include <boost/hana/functional/apply.hpp>

Initial value:
= [](auto&& f, auto&& ...x) -> decltype(auto) {
return forwarded(f)(forwarded(x)...);
}

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.

Parameters
fA 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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
static_assert(hana::apply(hana::plus, 1, 2) == 3, "");
int main() { }
Defines boost::hana::apply.
constexpr auto plus
Associative binary operation on a Monoid.
Definition: plus.hpp:47
constexpr auto apply
Invokes a Callable with the given arguments.
Definition: apply.hpp:40
Defines boost::hana::plus.

◆ arg

template<std::size_t n>
constexpr auto boost::hana::arg
constexpr

#include <boost/hana/functional/arg.hpp>

Initial value:
= [](auto&& x1, ..., auto&& xm) -> decltype(auto) {
return forwarded(xn);
}

Return the nth 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.

Template Parameters
nAn unsigned integer representing the argument to return. n must be positive (meaning nonzero).
Parameters
x1,...,xmA variadic pack of arguments from which the nth 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.

Rationale for <tt>n</tt> being a non-type template parameter

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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
// hana::arg<0>(1, '2', 3.3); // static assertion (regardless of the number of arguments)
static_assert(hana::arg<1>(1, '2', 3.3) == 1, "");
static_assert(hana::arg<2>(1, '2', 3.3) == '2', "");
static_assert(hana::arg<3>(1, '2', 3.3) == 3.3, "");
// hana::arg<4>(1, '2', 3.3); // static assertion
int main() { }
Defines boost::hana::arg.

◆ capture

constexpr auto boost::hana::capture
constexpr

#include <boost/hana/functional/capture.hpp>

Initial value:
= [](auto&& ...variables) {
return [perfect-capture](auto&& f) {
return [perfect-capture](auto&& ...args) -> decltype(auto) {
return forwarded(f)(forwarded(variables)..., forwarded(args)...);
};
};
}

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,

capture(vars...)(f)(args...) == f(vars..., args...)
Note
The arity of f must match the total number of arguments passed to it, i.e. sizeof...(vars) + sizeof...(args).

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTEXPR_LAMBDA auto sum = [](auto x, auto y, auto z) {
return x + y + z;
};
}
Defines macros to perform different kinds of assertions.
Defines boost::hana::capture.
Defines configuration macros used throughout the library.
constexpr auto sum
Compute the sum of the numbers of a structure.
Definition: sum.hpp:66
#define BOOST_HANA_CONSTEXPR_CHECK(...)
Equivalent to BOOST_HANA_CONSTEXPR_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERT...
Definition: assert.hpp:300

◆ compose

constexpr auto boost::hana::compose
constexpr

#include <boost/hana/functional/compose.hpp>

Initial value:
= [](auto&& f1, auto&& f2, ..., auto&& fn) {
return [perfect-capture](auto&& x, auto&& ...xs) -> decltype(auto) {
return forwarded(f1)(
forwarded(f2)(
...
forwarded(fn)(forwarded(x))
),
forwarded(xs)...
);
}
}

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

compose(f, g)(x, y...) == f(g(x), y...)
constexpr auto compose
Return the composition of two functions or more.
Definition: compose.hpp:52

If you need composition of the form f(g(x, y...)), use demux instead.

Note
compose is an associative operation; compose(f, compose(g, h)) is equivalent to compose(compose(f, g), h).
compose(f, compose(g, h))(x, xs...) == f(compose(g, h)(x), xs...)
== f(g(h(x)), xs...)
compose(compose(f, g), h)(x, xs...) == compose(f, g)(h(x), xs...)
== f(g(h(x)), xs...)

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTEXPR_LAMBDA auto to_char = [](int x) {
return static_cast<char>(x + 48);
};
BOOST_HANA_CONSTEXPR_LAMBDA auto increment = [](auto x) {
return x + 1;
};
BOOST_HANA_CONSTEXPR_CHECK(hana::compose(to_char, increment)(3) == '4');
}
Defines boost::hana::compose.

◆ curry

template<std::size_t n>
constexpr auto boost::hana::curry
constexpr

#include <boost/hana/functional/curry.hpp>

Initial value:
= [](auto&& f) {
return [perfect-capture](auto&& x1) {
return [perfect-capture](auto&& x2) {
...
return [perfect-capture](auto&& xn) -> decltype(auto) {
return forwarded(f)(
forwarded(x1), forwarded(x2), ..., forwarded(xn)
);
};
};
};
}

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

(X1, ..., Xn) -> R

Then, currying is the process of taking f and turning it into an equivalent function (call it g) of type

X1 -> (X2 -> (... -> (Xn -> R)))

This gives us the following equivalence, where x1, ..., xn are objects of type X1, ..., Xn respectively:

f(x1, ..., xn) == g(x1)...(xn)

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

curry<n>(f)(x1)...(xn) == f(x1, ..., xn)

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

curry<n>(f)(x1, ..., xk) == curry<n - k>(f)(x1)...(xk)

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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTEXPR_LAMBDA auto add = [](auto x, auto y, auto z) {
return x + y + z;
};
BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);
BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1)(2, 3) == hana::curry<3>(add)(1)(2)(3));
BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
// curry with a nullary function
BOOST_HANA_CONSTEXPR_LAMBDA auto two = []() {
return 2;
};
BOOST_HANA_CONSTEXPR_CHECK(hana::curry<0>(two)() == two());
}
Defines boost::hana::curry.

◆ mathtt

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 Iterables 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

demux(f)(g...)(x...) == f(g(x...)...)

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

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.

Note
When used with two functions only, 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)).

Signature

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,

iterate<n>(f, x) == f(f( ... f(x)))
^^^^^^^^^^ n times total

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

iterate<n>(f)(x) == iterate<n>(f, x)

This is provided for convenience, and it turns out to be especially useful in conjunction with higher-order algorithms.

Signature

Given a function \( f : T \to T \) and x and argument of data type T, the signature is

Signature

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.

Signature

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.

Note
All the sequences must have the same tag, and that tag must also match that of the top-level sequence.

Signature

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

chain(xs, f) == flatten(transform(xs, f))

Signature

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.

Signature

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.

Signature

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.

Signature

Given a Comonad W, the signature is

Signature

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

flatten(xs) == chain(xs, id)

For Sequences, this simply takes a Sequence of Sequences, and returns a (non-recursively) flattened Sequence.

Signature

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

f(... f(f(f(x1, x2), x3), x4) ..., xn) // without state
f(... f(f(f(f(state, x1), x2), x3), x4) ..., xn) // with state

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.

Signature

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

f(x1, f(x2, f(x3, f(x4, ... f(xn-1, xn) ... )))) // without state
f(x1, f(x2, f(x3, f(x4, ... f(xn, state) ... )))) // with state
Note
It is worth noting that the order in which the binary function should expect its arguments is reversed from 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.

Signature

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.

Signature

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.

Signature

Given a Hashable H, the signature is

Given two Iterables 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.

Note
This algorithm will short-circuit as soon as it can determine that one sequence is lexicographically less than the other. Hence, it can be used to compare infinite sequences. However, for the procedure to terminate on infinite sequences, the predicate has to be satisfied at a finite index.

Signature

Given two Iterables 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.

Signature

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.

Signature

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 chained into f instead of simply passed to it, as with normal composition. monadic_compose satisfies

monadic_compose(f, g)(x) == chain(g(x), f)
Note
Unlike compose, monadic_compose does not generalize nicely to arities higher than one. Hence, only unary functions may be used with monadic_compose.

Signature

Given a Monad M and two functions \( f : B \to M(C) \) and \( g : A \to M(B) \), the signature is

Note
This assumes the reader to be accustomed to non-monadic left-folds as explained by 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:

// with state
((((f(state, x1) | f(-, x2)) | f(-, x3)) | ...) | f(-, xn))
// without state
((((f(x1, x2) | f(-, x3)) | f(-, x4)) | ...) | f(-, xn))

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.

Signature

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

Note
This assumes the reader to be accustomed to non-monadic right-folds as explained by 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

// with state
(f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn, state)))))
// without state
(f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn-1, xn)))))

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.

Signature

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.

Signature

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:

Signature

Given F a Functor and U a type that can be compared with T, the signature is

Signature

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,

reverse_fold(sequence, state, f) == fold_right(sequence, state, flip(f))
reverse_fold(sequence, f) == fold_right(sequence, flip(f))
constexpr auto flip
Invoke a function with its two first arguments reversed.
Definition: flip.hpp:31
Note
This method is a convenience alias to fold_right. As an alias, reverse_fold is not tag-dispatched on its own and fold_right should be customized instead.

Signature

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.

Signature

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,

span(xs, predicate) == make_pair(take_while(xs, predicate),
drop_while(xs, predicate))
constexpr auto drop_while
Drop elements from an iterable up to, but excluding, the first element for which the predicate is not...
Definition: drop_while.hpp:44
constexpr auto take_while
Take elements from a sequence while the predicate is satisfied.
Definition: take_while.hpp:40

except that make_pair may be an arbitrary Product.

Signature

Given a Sequence S(T), a Logical Bool and a predicate \( T \to Bool \), span has the following signature:

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.

Signature

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.

Signature

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.

Signature

Given a Sequence S(T), a Logical Bool and a binary predicate \( T \times T \to Bool \), unique has the following signature:

◆ fix

constexpr auto boost::hana::fix
constexpr

#include <boost/hana/functional/fix.hpp>

Initial value:
= [](auto&& f) {
return [perfect-capture](auto&& ...x) -> decltype(auto) {
return forwarded(f)(fix(f), forwarded(x)...);
};
}
constexpr auto fix
Return a function computing the fixed point of a function.
Definition: fix.hpp:53

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

fix(f)(x...) == f(fix(f), x...)

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.

Parameters
fA function called as f(self, x...), where x... are the arguments in the fix(f)(x...) expression and self is fix(f).

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
BOOST_HANA_CONSTEXPR_STATELESS_LAMBDA auto factorial = hana::fix([](auto fact, auto n) -> int {
if (n == 0) return 1;
else return n * fact(n - 1);
});
int main() {
BOOST_HANA_CONSTEXPR_CHECK(factorial(5) == 120);
}
Defines boost::hana::fix.

◆ flip

constexpr auto boost::hana::flip
constexpr

#include <boost/hana/functional/flip.hpp>

Initial value:
= [](auto&& f) {
return [perfect-capture](auto&& x, auto&& y, auto&& ...z) -> decltype(auto) {
return forwarded(f)(forwarded(y), forwarded(x), forwarded(z)...);
};
}

Invoke a function with its two first arguments reversed.

Specifically, flip(f) is a function such that

flip(f)(x, y, z...) == f(y, x, z...)

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
BOOST_HANA_CONSTEXPR_LAMBDA auto minus = [](int x, int y, int z = 0) {
return x - y - z;
};
int main() {
BOOST_HANA_CONSTEXPR_CHECK(minus(3, 0, 1) == 3 - 0 - 1);
BOOST_HANA_CONSTEXPR_CHECK(hana::flip(minus)(3, 0, 1) == 0 - 3 - 1);
}
Defines boost::hana::flip.
constexpr auto minus
Subtract two elements of a group.
Definition: minus.hpp:51

◆ id

constexpr auto boost::hana::id
constexpr

#include <boost/hana/functional/id.hpp>

Initial value:
= [](auto&& x) -> decltype(auto) {
return forwarded(x);
}

The identity function – returns its argument unchanged.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
static_assert(hana::id(1) == 1, "");
static_assert(hana::id('x') == 'x', "");
int main() { }
constexpr auto id
The identity function – returns its argument unchanged.
Definition: id.hpp:23
Defines boost::hana::id.

◆ infix

constexpr auto boost::hana::infix
constexpr

#include <boost/hana/functional/infix.hpp>

Initial value:
= [](auto f) {
return unspecified;
}

Return an equivalent function that can also be applied in infix notation.

Specifically, infix(f) is an object such that:

infix(f)(x1, ..., xn) == f(x1, ..., xn)
x ^infix(f)^ y == f(x, y)
constexpr auto infix
Return an equivalent function that can also be applied in infix notation.
Definition: infix.hpp:79

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

(x ^ infix(f)) ^ y == x ^ (infix(f) ^ y)

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:

(infix(f) ^ x) ^ y; // compile-time assertion
y ^ (x ^ infix(f)); // compile-time assertion

Additionally, a function created with infix may be partially applied in infix notation. Specifically,

(x ^ infix(f))(y1, ..., yn) == f(x, y1, ..., yn)
(infix(f) ^ y)(x1, ..., xn) == f(x1, ..., xn, y)
  1. The ^ operator was chosen because it is left-associative and has a low enough priority so that most expressions will render the expected behavior.
  2. The operator can't be customimzed because that would require more sophistication in the implementation; I want to keep it as simple as possible. There is also an advantage in having a uniform syntax for infix application.
Parameters
fThe 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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
BOOST_HANA_CONSTEXPR_LAMBDA auto divmod = hana::infix([](auto x, auto y) {
// this could be a more efficient implementation
return hana::make_pair(x / y, x % y);
});
int main() {
BOOST_HANA_CONSTEXPR_CHECK((42 ^divmod^ 23) == hana::make_pair(1, 19));
}
Defines boost::hana::equal.
Defines boost::hana::infix.
Defines boost::hana::pair.

◆ lockstep

constexpr auto boost::hana::lockstep
constexpr

#include <boost/hana/functional/lockstep.hpp>

Initial value:
= [](auto&& f, auto&& ...g) {
return [perfect-capture](auto&& ...x) -> decltype(auto) {
return forwarded(f)(forwarded(g)(forwarded(x))...);
};
}

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

lockstep(f)(g1, ..., gN)(x1, ..., xN) == f(g1(x1), ..., gN(xN))
constexpr auto lockstep
Invoke a function with the result of invoking other functions on its arguments, in lockstep.
Definition: lockstep.hpp:39

Since each g is invoked on its corresponding argument in lockstep, the number of arguments must match the number of gs.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr int to_int(char c) {
return static_cast<int>(c) - 48;
}
constexpr int increment(int i) {
return i + 1;
}
static_assert(hana::lockstep(hana::plus)(to_int, increment)('3', 4) == 3 + 5, "");
int main() { }
Defines boost::hana::lockstep.

◆ on

constexpr auto boost::hana::on
constexpr

#include <boost/hana/functional/on.hpp>

Initial value:
= infix([](auto&& f, auto&& g) {
return [perfect-capture](auto&& ...x) -> decltype(auto) {
return forwarded(f)(g(forwarded(x))...);
};
})

Invoke a function with the result of invoking another function on each argument.

Specifically, on(f, g) is a function such that

on(f, g)(x...) == f(g(x)...)
constexpr auto on
Invoke a function with the result of invoking another function on each argument.
Definition: on.hpp:54

For convenience, on also supports infix application as provided by infix.

Note
on is associative, i.e. on(f, on(g, h)) is equivalent to on(on(f, g), h).
on(f, on(g, h))(xs...) == f(on(g, h)(xs)...)
== f(g(h(xs))...)
on(on(f, g), h)(xs...) == on(f, g)(h(xs)...)
== f(g(h(xs))...)

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
// infix application
constexpr auto sorted = hana::sort.by(hana::less ^hana::on^ hana::first, hana::make_tuple(
hana::make_pair(hana::int_c<3>, 'x'),
hana::make_pair(hana::int_c<1>, hana::type_c<void>),
hana::make_pair(hana::int_c<2>, 9876)
));
static_assert(sorted == hana::make_tuple(
hana::make_pair(hana::int_c<1>, hana::type_c<void>),
hana::make_pair(hana::int_c<2>, 9876),
hana::make_pair(hana::int_c<3>, 'x')
), "");
// function call syntax
constexpr auto x = hana::make_pair(1, 2);
constexpr auto y = hana::make_pair(10, 20);
static_assert(hana::on(hana::plus, hana::first)(x, y) == 1 + 10, "");
int main() { }
Defines boost::hana::first.
constexpr auto less
Returns a Logical representing whether x is less than y.
Definition: less.hpp:37
constexpr auto first
Returns the first element of a pair.
Definition: first.hpp:33
Defines boost::hana::integral_constant.
Defines boost::hana::less.
Defines boost::hana::on.
Defines boost::hana::sort.
Defines boost::hana::tuple.
Defines boost::hana::type and related utilities.

◆ overload

constexpr auto boost::hana::overload
constexpr

#include <boost/hana/functional/overload.hpp>

Initial value:
= [](auto&& f1, auto&& f2, ..., auto&& fn) {
return [perfect-capture](auto&& ...x) -> decltype(auto) {
return forwarded(fk)(forwarded(x)...);
};
}

Pick one of several functions to call based on overload resolution.

Specifically, overload(f1, f2, ..., fn) is a function object such that

overload(f1, f2, ..., fn)(x...) == fk(x...)
constexpr auto overload
Pick one of several functions to call based on overload resolution.
Definition: overload.hpp:35

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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <string>
namespace hana = boost::hana;
auto on_string = [](std::string const& s) {
std::cout << "matched std::string: " << s << std::endl;
return s;
};
auto on_int = [](int i) {
std::cout << "matched int: " << i << std::endl;
return i;
};
auto f = hana::overload(on_int, on_string);
int main() {
// prints "matched int: 1"
// prints "matched std::string: abcdef"
BOOST_HANA_RUNTIME_CHECK(f("abcdef") == std::string{"abcdef"});
}
#define BOOST_HANA_RUNTIME_CHECK(...)
Equivalent to BOOST_HANA_RUNTIME_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERTIO...
Definition: assert.hpp:209
Defines boost::hana::overload.

◆ overload_linearly

constexpr auto boost::hana::overload_linearly
constexpr

#include <boost/hana/functional/overload_linearly.hpp>

Initial value:
= [](auto&& f1, auto&& f2, ..., auto&& fn) {
return [perfect-capture](auto&& ...x) -> decltype(auto) {
return forwarded(fk)(forwarded(x)...);
};
}

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,

overload_linearly(f1, ..., fn)(args...) == fk(args...)
constexpr auto overload_linearly
Call the first function that produces a valid call expression.
Definition: overload_linearly.hpp:38

where fk is the first function such that fk(args...) is a valid expression.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <string>
namespace hana = boost::hana;
[](int i) { return i + 1; },
[](std::string s) { return s + "d"; },
[](double) { BOOST_HANA_RUNTIME_CHECK(false && "never called"); }
);
int main() {
BOOST_HANA_RUNTIME_CHECK(f("abc") == "abcd");
BOOST_HANA_RUNTIME_CHECK(f(2.2) == static_cast<int>(2.2) + 1);
}
Defines boost::hana::overload_linearly.

◆ partial

constexpr auto boost::hana::partial
constexpr

#include <boost/hana/functional/partial.hpp>

Initial value:
= [](auto&& f, auto&& ...x) {
return [perfect-capture](auto&& ...y) -> decltype(auto) {
return forwarded(f)(forwarded(x)..., forwarded(y)...);
};
}

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

partial(f, x...)(y...) == f(x..., y...)
constexpr auto partial
Partially apply a function to some arguments.
Definition: partial.hpp:43
Note
The arity of f must match the total number of arguments passed to it, i.e. sizeof...(x) + sizeof...(y).

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto increment = hana::partial(hana::plus, 1);
static_assert(increment(2) == 3, "");
int main() { }
Defines boost::hana::partial.

◆ _

constexpr unspecified boost::hana::_ {}
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 @:

(_ @ _)(x, y) == x @ y
constexpr unspecified _
Create simple functions representing C++ operators inline.
Definition: placeholder.hpp:70

Operators may also be partially applied to one argument inline:

(x @ _)(y) == x @ y
(_ @ y)(x) == x @ y

When invoked with more arguments than required, functions created with _ will discard the superfluous instead of triggering an error:

(_ @ _)(x, y, z...) == x @ y

This makes functions created with _ easier to use in higher-order algorithms, which sometime provide more information than necessary to their callbacks.

Supported operators

  • Arithmetic: binary +, binary -, /, *, %, unary +, unary -
  • Bitwise: ~, &, |, ^, <<, >>
  • Comparison: ==, !=, <, <=, >, >=
  • Logical: ||, &&, !
  • Member access: * (dereference), [] (array subscript)
  • Other: () (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.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto plus = hana::_ + hana::_;
static_assert(plus(1, 2) == 1 + 2, "");
constexpr auto increment = hana::_ + 1;
static_assert(increment(1) == 2, "");
constexpr auto twice = 2 * hana::_;
static_assert(twice(1) == 2, "");
// Extra arguments are ignored.
static_assert(twice(1, "ignored") == 2, "");
int main() { }
Defines boost::hana::_.

◆ reverse_partial

constexpr auto boost::hana::reverse_partial
constexpr

#include <boost/hana/functional/reverse_partial.hpp>

Initial value:
= [](auto&& f, auto&& ...x) {
return [perfect-capture](auto&& ...y) -> decltype(auto) {
return forwarded(f)(forwarded(y)..., forwarded(x)...);
};
}

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

reverse_partial(f, x...)(y...) == f(y..., x...)
constexpr auto reverse_partial
Partially apply a function to some arguments.
Definition: reverse_partial.hpp:42
Note
The arity of f must match the total number of arguments passed to it, i.e. sizeof...(x) + sizeof...(y).

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto half = hana::reverse_partial(hana::div, 2);
static_assert(half(4) == 2, "");
static_assert(half(8) == 4, "");
int main() { }
Defines boost::hana::div.
constexpr auto div
Generalized integer division.
Definition: div.hpp:43
Defines boost::hana::reverse_partial.