Boost.Hana  1.7.0 Your standard library for metaprogramming
Sequence

## Description

The Sequence concept represents generic index-based sequences.

Compared to other abstract concepts, the Sequence concept is very specific. It represents generic index-based sequences. The reason why such a specific concept is provided is because there are a lot of models that behave exactly the same while being implemented in wildly different ways. It is useful to regroup all those data types under the same umbrella for the purpose of generic programming.

In fact, models of this concept are not only similar. They are actually isomorphic, in a sense that we define below, which is a fancy way of rigorously saying that they behave exactly the same to an external observer.

## Minimal complete definition

Iterable, Foldable, and make

The Sequence concept does not provide basic methods that could be used as a minimal complete definition; instead, it borrows methods from other concepts and add laws to them. For this reason, it is necessary to specialize the Sequence metafunction in Hana's namespace to tell Hana that a type is indeed a Sequence. Explicitly specializing the Sequence metafunction can be seen like a seal saying "this data type satisfies the additional laws of a Sequence", since those can't be checked by Hana automatically.

## Laws

The laws for being a Sequence are simple, and their goal is to restrict the semantics that can be associated to the functions provided by other concepts. First, a Sequence must be a finite Iterable (thus a Foldable too). Secondly, for a Sequence tag S, make<S>(x1, ..., xn) must be an object of tag S and whose linearization is [x1, ..., xn]. This basically ensures that objects of tag S are equivalent to their linearization, and that they can be created from such a linearization (with make).

While it would be possible in theory to handle infinite sequences, doing so complicates the implementation of many algorithms. For simplicity, the current version of the library only handles finite sequences. However, note that this does not affect in any way the potential for having infinite Searchables and Iterables.

## Refined concepts

1. Comparable (definition provided automatically)
Two Sequences are equal if and only if they contain the same number of elements and their elements at any given index are equal.
namespace hana = boost::hana;
static_assert(hana::make_tuple(1, 2, 3) == hana::make_tuple(1, 2, 3), "");
BOOST_HANA_CONSTANT_CHECK(hana::make_tuple(1, 2, 3) != hana::make_tuple(1, 2, 3, 4));
int main() { }
2. Orderable (definition provided automatically)
Sequences are ordered using the traditional lexicographical ordering.
namespace hana = boost::hana;
static_assert(hana::make_tuple(1, 2, 3) < hana::make_tuple(2, 3, 4), "");
static_assert(hana::make_tuple(1, 2, 3) < hana::make_tuple(1, 2, 3, 4), "");
int main() { }
3. Functor (definition provided automatically)
Sequences implement transform as the mapping of a function over each element of the sequence. This is somewhat equivalent to what std::transform does to ranges of iterators. Also note that mapping a function over an empty sequence returns an empty sequence and never applies the function, as would be expected.
#include <sstream>
#include <string>
namespace hana = boost::hana;
auto to_string = [](auto x) {
std::ostringstream ss;
ss << x;
return ss.str();
};
int main() {
hana::transform(hana::make_tuple(1, '2', "345", std::string{"67"}), to_string) ==
hana::make_tuple("1", "2", "345", "67")
);
}
4. Applicative (definition provided automatically)
First, lifting a value into a Sequence is the same as creating a singleton sequence containing that value. Second, applying a sequence of functions to a sequence of values will apply each function to all the values in the sequence, and then return a list of all the results. In other words,
ap([f1, ..., fN], [x1, ..., xM]) == [
f1(x1), ..., f1(xM),
...
fN(x1), ..., fN(xM)
]
Example:
#include <tuple>
namespace hana = boost::hana;
static_assert(hana::lift<hana::tuple_tag>('x') == hana::make_tuple('x'), "");
static_assert(hana::equal(hana::lift<hana::ext::std::tuple_tag>('x'), std::make_tuple('x')), "");
constexpr auto f = hana::make_pair;
constexpr auto g = hana::flip(hana::make_pair);
static_assert(
hana::ap(hana::make_tuple(f, g), hana::make_tuple(1, 2, 3), hana::make_tuple('a', 'b'))
==
hana::make_tuple(
f(1, 'a'), f(1, 'b'), f(2, 'a'), f(2, 'b'), f(3, 'a'), f(3, 'b'),
g(1, 'a'), g(1, 'b'), g(2, 'a'), g(2, 'b'), g(3, 'a'), g(3, 'b')
)
, "");
int main() { }
First, flatenning a Sequence takes a sequence of sequences and concatenates them to get a larger sequence. In other words,
flatten([[a1, ..., aN], ..., [z1, ..., zM]]) == [
a1, ..., aN, ..., z1, ..., zM
]
This acts like a std::tuple_cat function, except it receives a sequence of sequences instead of a variadic pack of sequences to flatten.
Example:
namespace hana = boost::hana;
static_assert(
hana::flatten(hana::make_tuple(
hana::make_tuple(1, 2),
hana::make_tuple(3, 4),
hana::make_tuple(hana::make_tuple(5, 6))
))
== hana::make_tuple(1, 2, 3, 4, hana::make_tuple(5, 6))
, "");
int main() { }
Also note that the model of Monad for Sequences can be seen as modeling nondeterminism. A nondeterministic computation can be modeled as a function which returns a sequence of possible results. In this line of thought, chaining a sequence of values into such a function will return a sequence of all the possible output values, i.e. a sequence of all the values applied to all the functions in the sequences.
Example:
#include <type_traits>
#include <utility>
namespace hana = boost::hana;
// Using the tuple Monad, we generate all the possible combinations of
// cv-qualifiers and reference qualifiers. Then, we use the optional
// Monad to make sure that our generic function can be called with
// arguments of any of those types.
// cv_qualifiers : type -> tuple(type)
auto cv_qualifiers = [](auto t) {
return hana::make_tuple(
t,
);
};
// ref_qualifiers : type -> tuple(type)
auto ref_qualifiers = [](auto t) {
return hana::make_tuple(
);
};
auto possible_args = cv_qualifiers(hana::type_c<int>) | ref_qualifiers;
possible_args == hana::make_tuple(
hana::type_c<int&>,
hana::type_c<int&&>,
hana::type_c<int const&>,
hana::type_c<int const&&>,
hana::type_c<int volatile&>,
hana::type_c<int volatile&&>,
hana::type_c<int const volatile&>,
hana::type_c<int const volatile&&>
)
);
struct some_function {
template <typename T>
void operator()(T&&) const { }
};
int main() {
hana::for_each(possible_args, [](auto t) {
using T = typename decltype(t)::type;
static_assert(decltype(hana::is_valid(some_function{})(std::declval<T>())){},
"some_function should be callable with any type of argument");
});
}
Sequences are models of the MonadPlus concept by considering the empty sequence as the unit of concat, and sequence concatenation as concat.
#include <string>
namespace hana = boost::hana;
using namespace std::string_literals;
BOOST_HANA_CONSTANT_CHECK(hana::empty<hana::tuple_tag>() == hana::make_tuple());
static_assert(hana::append(hana::make_tuple(1, '2', 3.3), nullptr)
== hana::make_tuple(1, '2', 3.3, nullptr), "");
int main() {
hana::concat(hana::make_tuple(1, '2', 3.3), hana::make_tuple("abcdef"s)) ==
hana::make_tuple(1, '2', 3.3, "abcdef"s)
);
}
7. Foldable
The model of Foldable for Sequences is uniquely determined by the model of Iterable.
#include <sstream>
namespace hana = boost::hana;
auto to_string = [](auto x) {
std::ostringstream ss;
ss << x;
return ss.str();
};
auto show = [](auto x, auto y) {
return "(" + to_string(x) + " + " + to_string(y) + ")";
};
int main() {
hana::fold_left(hana::make_tuple(2, "3", '4'), "1", show) == "(((1 + 2) + 3) + 4)"
);
}
8. Iterable
The model of Iterable for Sequences corresponds to iteration over each element of the sequence, in order. This model is not provided automatically, and it is in fact part of the minimal complete definition for the Sequence concept.
namespace hana = boost::hana;
static_assert(hana::front(hana::make_tuple(1, '2', 3.3)) == 1, "");
static_assert(hana::drop_front(hana::make_tuple(1, '2', 3.3)) == hana::make_tuple('2', 3.3), "");
BOOST_HANA_CONSTANT_CHECK(!hana::is_empty(hana::make_tuple(1, '2', 3.3)));
int main() { }
9. Searchable (definition provided automatically)
Searching through a Sequence is equivalent to just searching through a list of the values it contains. The keys and the values on which the search is performed are both the elements of the sequence.
#include <string>
namespace hana = boost::hana;
using namespace std::string_literals;
int main() {
hana::find_if(hana::make_tuple(1, '2', 3.3, "abc"s), hana::is_a<std::string>) == hana::just("abc"s)
);
"abc"s ^hana::in^ hana::make_tuple(1, '2', 3.3, "abc"s)
);
}

## Variables

constexpr auto boost::hana::drop_back
Drop the last n elements of a finite sequence, and return the rest. More...

constexpr insert_t boost::hana::insert {}
Insert a value at a given index in a sequence. More...

constexpr auto boost::hana::insert_range
Insert several values at a given index in a sequence. More...

constexpr auto boost::hana::intersperse
Insert a value between each pair of elements in a finite sequence. More...

constexpr auto boost::hana::permutations
Return a sequence of all the permutations of the given sequence. More...

constexpr auto boost::hana::remove_at
Remove the element at a given index from a sequence. More...

template<std::size_t n>
constexpr auto boost::hana::remove_at_c
Equivalent to remove_at; provided for convenience. More...

constexpr auto boost::hana::remove_range
Remove the elements inside a given range of indices from a sequence. More...

template<std::size_t from, std::size_t to>
constexpr auto boost::hana::remove_range_c
Equivalent to remove_range; provided for convenience. More...

constexpr auto boost::hana::reverse
Reverse a sequence. More...

constexpr auto boost::hana::scan_left
Fold a Sequence to the left and return a list containing the successive reduction states. More...

constexpr auto boost::hana::scan_right
Fold a Sequence to the right and return a list containing the successive reduction states. More...

constexpr auto boost::hana::slice
Extract the elements of a Sequence at the given indices. More...

template<std::size_t from, std::size_t to>
constexpr auto boost::hana::slice_c
Shorthand to slice a contiguous range of elements. More...

constexpr auto boost::hana::take_back
Returns the last n elements of a sequence, or the whole sequence if the sequence has less than n elements. More...

template<std::size_t n>
constexpr auto boost::hana::take_back_c
Equivalent to take_back; provided for convenience. More...

constexpr auto boost::hana::take_front
Returns the first n elements of a sequence, or the whole sequence if the sequence has less than n elements. More...

template<std::size_t n>
constexpr auto boost::hana::take_front_c
Equivalent to take_front; provided for convenience. More...

constexpr auto boost::hana::take_while
Take elements from a sequence while the predicate is satisfied. More...

constexpr auto boost::hana::zip
Zip one sequence or more. More...

constexpr auto boost::hana::zip_shortest
Zip one sequence or more. More...

constexpr auto boost::hana::zip_shortest_with
Zip one sequence or more with a given function. More...

constexpr auto boost::hana::zip_with
Zip one sequence or more with a given function. More...

## ◆ drop_back

 constexpr auto boost::hana::drop_back
constexpr

#include <boost/hana/fwd/drop_back.hpp>

Initial value:
= [](auto&& xs[, auto const& n]) {
return tag-dispatched;
}

Drop the last n elements of a finite sequence, and return the rest.

Given a finite Sequence xs with a linearization of [x1, ..., xm] and a non-negative IntegralConstant n, drop_back(xs, n) is a sequence with the same tag as xs whose linearization is [x1, ..., xm-n]. If n is not given, it defaults to an IntegralConstant with a value equal to 1.

In case length(xs) <= n, drop_back will simply drop the whole sequence without failing, thus returning an empty sequence.

Parameters
 xs The sequence from which elements are dropped. n A non-negative IntegralConstant representing the number of elements to be dropped from the end of the sequence. If n is not given, it defaults to an IntegralConstant with a value equal to 1.

## Example

namespace hana = boost::hana;
constexpr auto xs = hana::make_tuple(0, '1', 2.0);
static_assert(hana::drop_back(xs, hana::size_c<0>) == xs, "");
static_assert(hana::drop_back(xs, hana::size_c<1>) == hana::make_tuple(0, '1'), "");
static_assert(hana::drop_back(xs, hana::size_c<2>) == hana::make_tuple(0), "");
BOOST_HANA_CONSTANT_CHECK(hana::drop_back(xs, hana::size_c<3>) == hana::make_tuple());
BOOST_HANA_CONSTANT_CHECK(hana::drop_back(xs, hana::size_c<4>) == hana::make_tuple());
static_assert(hana::drop_back(xs) == hana::make_tuple(0, '1'), "");
int main() { }

## ◆ insert

 constexpr auto boost::hana::insert {}
related

#include <boost/hana/fwd/insert.hpp>

Insert a value at a given index in a sequence.

Given a sequence, an index and an element to insert, insert inserts the element at the given index.

Parameters
 xs The sequence in which a value should be inserted. n The index at which an element should be inserted. This must be a non-negative Constant of an integral type, and it must also be true that n < length(xs) if xs is a finite sequence. element The element to insert in the sequence.

## Example

#include <string>
namespace hana = boost::hana;
using namespace hana::literals;
using namespace std::literals;
int main() {
auto xs = hana::make_tuple("Hello"s, "world!"s);
hana::insert(xs, 1_c, " "s) == hana::make_tuple("Hello"s, " "s, "world!"s)
);
}

## ◆ insert_range

 constexpr auto boost::hana::insert_range
constexpr

#include <boost/hana/fwd/insert_range.hpp>

Initial value:
= [](auto&& xs, auto&& n, auto&& elements) {
return tag-dispatched;
}

Insert several values at a given index in a sequence.

Given a sequence, an index and any Foldable containing elements to insert, insert_range inserts the elements in the Foldable at the given index of the sequence.

Parameters
 xs The sequence in which values should be inserted. n The index at which elements should be inserted. This must be a non-negative Constant of an integral type, and it must also be true that n < length(xs) if xs is a finite sequence. elements A Foldable containing elements to insert in the sequence.

## Example

#include <string>
namespace hana = boost::hana;
using namespace hana::literals;
using namespace std::literals;
int main() {
auto xs = hana::make_tuple("Hello"s, "world!"s);
hana::insert_range(xs, 1_c, hana::make_tuple(1, 2, 3)) == hana::make_tuple("Hello"s, 1, 2, 3, "world!"s)
);
}

## ◆ intersperse

 constexpr auto boost::hana::intersperse
constexpr

#include <boost/hana/fwd/intersperse.hpp>

Initial value:
= [](auto&& xs, auto&& z) {
return tag-dispatched;
}

Insert a value between each pair of elements in a finite sequence.

Given a finite Sequence xs with a linearization of [x1, x2, ..., xn], intersperse(xs, z) is a new sequence with a linearization of [x1, z, x2, z, x3, ..., xn-1, z, xn]. In other words, it inserts the z element between every pair of elements of the original sequence. If the sequence is empty or has a single element, intersperse returns the sequence as-is. In all cases, the sequence must be finite.

Parameters
 xs The sequence in which a value is interspersed. z The value to be inserted between every pair of elements of the sequence.

## Example

namespace hana = boost::hana;
static_assert(hana::intersperse(hana::make_tuple(1, '2', 3.3), 'x') == hana::make_tuple(1, 'x', '2', 'x', 3.3), "");
BOOST_HANA_CONSTANT_CHECK(hana::intersperse(hana::make_tuple(), 'x') == hana::make_tuple());
int main() { }

## ◆ permutations

 constexpr auto boost::hana::permutations
constexpr

#include <boost/hana/fwd/permutations.hpp>

Initial value:
= [](auto&& xs) {
return tag-dispatched;
}

Return a sequence of all the permutations of the given sequence.

Specifically, permutations(xs) is a sequence whose elements are permutations of the original sequence xs. The permutations are not guaranteed to be in any specific order. Also note that the number of permutations grows very rapidly as the length of the original sequence increases. The growth rate is O(length(xs)!); with a sequence xs of length only 8, permutations(xs) contains over 40 000 elements!

## Example

namespace hana = boost::hana;
BOOST_HANA_CONSTEXPR_LAMBDA auto is_permutation_of = hana::curry<2>([](auto xs, auto perm) {
});
int main() {
hana::make_tuple(
hana::make_tuple('1', 2, 3.0),
hana::make_tuple('1', 3.0, 2),
hana::make_tuple(2, '1', 3.0),
hana::make_tuple(2, 3.0, '1'),
hana::make_tuple(3.0, '1', 2),
hana::make_tuple(3.0, 2, '1')
),
is_permutation_of(hana::make_tuple('1', 2, 3.0))
)
);
}

## ◆ remove_at

 constexpr auto boost::hana::remove_at
constexpr

#include <boost/hana/fwd/remove_at.hpp>

Initial value:
= [](auto&& xs, auto const& n) {
return tag-dispatched;
}

Remove the element at a given index from a sequence.

remove_at returns a new sequence identical to the original, except that the element at the given index is removed. Specifically, remove_at([x0, ..., xn-1, xn, xn+1, ..., xm], n) is a new sequence equivalent to [x0, ..., xn-1, xn+1, ..., xm].

Note
The behavior is undefined if the index is out of the bounds of the sequence.
Parameters
 xs A sequence from which an element is to be removed. n An non-negative IntegralConstant representing the index of the element to be removed from the sequence. The behavior is undefined if that index is not in the bounds of the sequence.

## Example

namespace hana = boost::hana;
constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3u);
static_assert(hana::remove_at(xs, hana::size_c<2>) == hana::make_tuple(0, '1', 3u), "");
int main() { }

## ◆ remove_at_c

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

#include <boost/hana/fwd/remove_at.hpp>

Initial value:
= [](auto&& xs) {
return hana::remove_at(forwarded(xs), hana::size_c<n>);
}

Equivalent to remove_at; provided for convenience.

## Example

namespace hana = boost::hana;
constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3u);
static_assert(hana::remove_at_c<2>(xs) == hana::make_tuple(0, '1', 3u), "");
int main() { }

## ◆ remove_range

 constexpr auto boost::hana::remove_range
constexpr

#include <boost/hana/fwd/remove_range.hpp>

Initial value:
= [](auto&& xs, auto const& from, auto const& to) {
return tag-dispatched;
}

Remove the elements inside a given range of indices from a sequence.

remove_range returns a new sequence identical to the original, except that elements at indices in the provided range are removed. Specifically, remove_range([x0, ..., xn], from, to) is a new sequence equivalent to [x0, ..., x_from-1, x_to, ..., xn].

Note
The behavior is undefined if the range contains any index out of the bounds of the sequence.
Parameters
 xs A sequence from which elements are removed. [from,to) An half-open interval of IntegralConstants representing the indices of the elements to be removed from the sequence. The IntegralConstants in the half-open interval must be non-negative and in the bounds of the sequence. The half-open interval must also be valid, meaning that from <= to.

## Example

namespace hana = boost::hana;
constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3u, 4, 5.5);
static_assert(hana::remove_range(xs, hana::size_c<2>, hana::size_c<4>) == hana::make_tuple(0, '1', 4, 5.5), "");
int main() { }

## ◆ remove_range_c

template<std::size_t from, std::size_t to>
 constexpr auto boost::hana::remove_range_c
constexpr

#include <boost/hana/fwd/remove_range.hpp>

Initial value:
= [](auto&& xs) {
return hana::remove_range(forwarded(xs), hana::size_c<from>, hana::size_c<to>);
}

Equivalent to remove_range; provided for convenience.

## Example

namespace hana = boost::hana;
constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3u, 4, 5.5);
static_assert(hana::remove_range_c<2, 4>(xs) == hana::make_tuple(0, '1', 4, 5.5), "");
int main() { }

## ◆ reverse

 constexpr auto boost::hana::reverse
constexpr

#include <boost/hana/fwd/reverse.hpp>

Initial value:
= [](auto&& xs) {
return tag-dispatched;
}

Reverse a sequence.

Specifically, reverse(xs) is a new sequence containing the same elements as xs, except in reverse order.

Parameters
 xs The sequence to reverse.

## Example

namespace hana = boost::hana;
static_assert(hana::reverse(hana::make_tuple(1, '2', 3.3)) == hana::make_tuple(3.3, '2', 1), "");
int main() { }

## ◆ scan_left

 constexpr auto boost::hana::scan_left
constexpr

#include <boost/hana/fwd/scan_left.hpp>

Initial value:
= [](auto&& xs[, auto&& state], auto const& f) {
return tag-dispatched;
}

Fold a Sequence to the left and return a list containing the successive reduction states.

Like fold_left, scan_left reduces a sequence to a single value using a binary operation. However, unlike fold_left, it builds up a sequence of the intermediary results computed along the way and returns that instead of only the final reduction state. Like fold_left, scan_left can be used with or without an initial reduction state.

When the sequence is empty, two things may arise. If an initial state was provided, a singleton list containing that state is returned. Otherwise, if no initial state was provided, an empty list is returned. In particular, unlike for fold_left, using scan_left on an empty sequence without an initial state is not an error.

More specifically, scan_left([x1, ..., xn], state, f) is a sequence whose ith element is equivalent to fold_left([x1, ..., xi], state, f). The no-state variant is handled in an analogous way. For illustration, consider this left fold on a short sequence:

fold_left([x1, x2, x3], state, f) == f(f(f(state, x1), x2), x3)

The analogous sequence generated with scan_left will be

scan_left([x1, x2, x3], state, f) == [
state,
f(state, x1),
f(f(state, x1), x2),
f(f(f(state, x1), x2), x3)
]

Similarly, consider this left fold (without an initial state) on a short sequence:

fold_left([x1, x2, x3, x4], f) == f(f(f(x1, x2), x3), x4)

The analogous sequence generated with scan_left will be

scan_left([x1, x2, x3, x4], f) == [
x1,
f(x1, x2),
f(f(x1, x2), x3),
f(f(f(x1, x2), x3), x4)
]
Parameters
 xs The sequence to scan from the left. state The (optional) initial reduction state. f A binary function called as f(state, x), where state is the result accumulated so far and x is an element in the sequence. If no initial state is provided, f is called as f(x1, x2), where x1 and x2 are both elements of the sequence.

## Example

#include <sstream>
namespace hana = boost::hana;
auto to_string = [](auto x) {
std::ostringstream ss;
ss << x;
return ss.str();
};
auto f = [](auto state, auto element) {
return "f(" + to_string(state) + ", " + to_string(element) + ")";
};
int main() {
// with initial state
BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(2, "3", '4'), 1, f) == hana::make_tuple(
1,
"f(1, 2)",
"f(f(1, 2), 3)",
"f(f(f(1, 2), 3), 4)"
));
// without initial state
BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple(
1,
"f(1, 2)",
"f(f(1, 2), 3)"
));
}

## ◆ scan_right

 constexpr auto boost::hana::scan_right
constexpr

#include <boost/hana/fwd/scan_right.hpp>

Initial value:
= [](auto&& xs[, auto&& state], auto const& f) {
return tag-dispatched;
}

Fold a Sequence to the right and return a list containing the successive reduction states.

Like fold_right, scan_right reduces a sequence to a single value using a binary operation. However, unlike fold_right, it builds up a sequence of the intermediary results computed along the way and returns that instead of only the final reduction state. Like fold_right, scan_right can be used with or without an initial reduction state.

When the sequence is empty, two things may arise. If an initial state was provided, a singleton list containing that state is returned. Otherwise, if no initial state was provided, an empty list is returned. In particular, unlike for fold_right, using scan_right on an empty sequence without an initial state is not an error.

More specifically, scan_right([x1, ..., xn], state, f) is a sequence whose ith element is equivalent to fold_right([x1, ..., xi], state, f). The no-state variant is handled in an analogous way. For illustration, consider this right fold on a short sequence:

fold_right([x1, x2, x3], state, f) == f(x1, f(x2, f(x3, state)))

The analogous sequence generated with scan_right will be

scan_right([x1, x2, x3], state, f) == [
f(x1, f(x2, f(x3, state))),
f(x2, f(x3, state)),
f(x3, state),
state
]

Similarly, consider this right fold (without an initial state) on a short sequence:

fold_right([x1, x2, x3, x4], f) == f(x1, f(x2, f(x3, x4)))

The analogous sequence generated with scan_right will be

scan_right([x1, x2, x3, x4], f) == [
f(x1, f(x2, f(x3, x4))),
f(x2, f(x3, x4)),
f(x3, x4),
x4
]
Parameters
 xs The sequence to scan from the right. state The (optional) initial reduction state. f A binary function called as f(x, state), where state is the result accumulated so far and x is an element in the sequence. When no initial state is provided, f is called as f(x1, x2), where x1 and x2 are elements of the sequence.

## Example

#include <sstream>
namespace hana = boost::hana;
auto to_string = [](auto x) {
std::ostringstream ss;
ss << x;
return ss.str();
};
auto f = [](auto element, auto state) {
return "f(" + to_string(element) + ", " + to_string(state) + ")";
};
int main() {
// with initial state
BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), 4, f) == hana::make_tuple(
"f(1, f(2, f(3, 4)))",
"f(2, f(3, 4))",
"f(3, 4)",
4
));
// without initial state
BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple(
"f(1, f(2, 3))",
"f(2, 3)",
'3'
));
}

## ◆ slice

 constexpr auto boost::hana::slice
constexpr

#include <boost/hana/fwd/slice.hpp>

Initial value:
= [](auto&& xs, auto&& indices) {
return tag-dispatched;
}

Extract the elements of a Sequence at the given indices.

Given an arbitrary sequence of indices, slice returns a new sequence of the elements of the original sequence that appear at those indices. In other words,

slice([x1, ..., xn], [i1, ..., ik]) == [x_i1, ..., x_ik]

The indices do not have to be ordered or contiguous in any particular way, but they must not be out of the bounds of the sequence. It is also possible to specify the same index multiple times, in which case the element at this index will be repeatedly included in the resulting sequence.

Parameters
 xs The sequence from which a subsequence is extracted. indices A compile-time Foldable containing non-negative IntegralConstants representing the indices. The indices are 0-based, and they must all be in bounds of the xs sequence. Note that any Foldable will really do (no need for an Iterable, for example); the linearization of the indices is used to determine the order of the elements included in the slice.

## Example

namespace hana = boost::hana;
using namespace hana::literals;
// Slice a contiguous range
constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3_c, hana::type_c<float>);
static_assert(
hana::slice(xs, hana::tuple_c<std::size_t, 1, 2, 3>) ==
hana::make_tuple('1', 2.2, 3_c)
, "");
// A more complex example with a non-contiguous range
constexpr auto letters = hana::to_tuple(hana::range_c<char, 'a', 'z'>);
constexpr auto indices = hana::to_tuple(hana::make_range(hana::size_c<0>, hana::length(letters)));
auto even_indices = hana::filter(indices, [](auto n) {
return n % hana::size_c<2> == hana::size_c<0>;
});
hana::slice(letters, even_indices) == hana::tuple_c<char,
'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y'
>
);
int main() { }

## ◆ slice_c

template<std::size_t from, std::size_t to>
 constexpr auto boost::hana::slice_c
constexpr

#include <boost/hana/fwd/slice.hpp>

Initial value:
= [](auto&& xs) {
return hana::slice(forwarded(xs), hana::range_c<std::size_t, from, to>);
}

Shorthand to slice a contiguous range of elements.

slice_c is simply a shorthand to slice a contiguous range of elements. In particular, slice_c<from, to>(xs) is equivalent to slice(xs, range_c<std::size_t, from, to>), which simply slices all the elements of xs contained in the half-open interval delimited by [from, to). Like for slice, the indices used with slice_c are 0-based and they must be in the bounds of the sequence being sliced.

Template Parameters
 from The index of the first element in the slice. to One-past the index of the last element in the slice. It must hold that from <= to.

## Example

namespace hana = boost::hana;
static_assert(
hana::slice_c<1, 3>(hana::make_tuple(1, '2', 3.3, hana::type_c<float>))
==
hana::make_tuple('2', 3.3)
, "");
int main() { }

## ◆ take_back

 constexpr auto boost::hana::take_back
constexpr

#include <boost/hana/fwd/take_back.hpp>

Initial value:
= [](auto&& xs, auto const& n) {
return tag-dispatched;
}

Returns the last n elements of a sequence, or the whole sequence if the sequence has less than n elements.

Given a Sequence xs and an IntegralConstant n, take_back(xs, n) is a new sequence containing the last n elements of xs, in the same order. If length(xs) <= n, the whole sequence is returned and no error is triggered.

Parameters
 xs The sequence to take the elements from. n A non-negative IntegralConstant representing the number of elements to keep in the resulting sequence.

## Example

namespace hana = boost::hana;
BOOST_HANA_CONSTANT_CHECK(hana::take_back(hana::make_tuple(1, '2', 3.3), hana::size_c<0>) == hana::make_tuple());
static_assert(hana::take_back(hana::make_tuple(1, '2', 3.3), hana::size_c<1>) == hana::make_tuple(3.3), "");
static_assert(hana::take_back(hana::make_tuple(1, '2', 3.3), hana::size_c<2>) == hana::make_tuple('2', 3.3), "");
static_assert(hana::take_back(hana::make_tuple(1, '2', 3.3), hana::size_c<3>) == hana::make_tuple(1, '2', 3.3), "");
static_assert(hana::take_back(hana::make_tuple(1, '2', 3.3), hana::size_c<4>) == hana::make_tuple(1, '2', 3.3), "");
int main() { }

## ◆ take_back_c

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

#include <boost/hana/fwd/take_back.hpp>

Initial value:
= [](auto&& xs) {
return hana::take_back(forwarded(xs), hana::size_c<n>);
}

Equivalent to take_back; provided for convenience.

## Example

namespace hana = boost::hana;
static_assert(hana::take_back_c<2>(hana::make_tuple(1, '2', 3.3)) == hana::make_tuple('2', 3.3), "");
int main() { }

## ◆ take_front

 constexpr auto boost::hana::take_front
constexpr

#include <boost/hana/fwd/take_front.hpp>

Initial value:
= [](auto&& xs, auto const& n) {
return tag-dispatched;
}

Returns the first n elements of a sequence, or the whole sequence if the sequence has less than n elements.

Given a Sequence xs and an IntegralConstant n, take_front(xs, n) is a new sequence containing the first n elements of xs, in the same order. If length(xs) <= n, the whole sequence is returned and no error is triggered.

Parameters
 xs The sequence to take the elements from. n A non-negative IntegralConstant representing the number of elements to keep in the resulting sequence.

## Example

namespace hana = boost::hana;
BOOST_HANA_CONSTANT_CHECK(hana::take_front(hana::make_tuple(1, '2', 3.3), hana::size_c<0>) == hana::make_tuple());
static_assert(hana::take_front(hana::make_tuple(1, '2', 3.3), hana::size_c<1>) == hana::make_tuple(1), "");
static_assert(hana::take_front(hana::make_tuple(1, '2', 3.3), hana::size_c<2>) == hana::make_tuple(1, '2'), "");
static_assert(hana::take_front(hana::make_tuple(1, '2', 3.3), hana::size_c<3>) == hana::make_tuple(1, '2', 3.3), "");
static_assert(hana::take_front(hana::make_tuple(1, '2', 3.3), hana::size_c<4>) == hana::make_tuple(1, '2', 3.3), "");
int main() { }

## ◆ take_front_c

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

#include <boost/hana/fwd/take_front.hpp>

Initial value:
= [](auto&& xs) {
return hana::take_front(forwarded(xs), hana::size_c<n>);
}

Equivalent to take_front; provided for convenience.

## Example

namespace hana = boost::hana;
static_assert(hana::take_front_c<2>(hana::make_tuple(1, '2', 3.3)) == hana::make_tuple(1, '2'), "");
int main() { }

## ◆ take_while

 constexpr auto boost::hana::take_while
constexpr

#include <boost/hana/fwd/take_while.hpp>

Initial value:
= [](auto&& xs, auto&& predicate) {
return tag-dispatched;
}

Take elements from a sequence while the predicate is satisfied.

Specifically, take_while returns a new sequence containing the longest prefix of xs in which all the elements satisfy the given predicate.

Parameters
 xs The sequence to take elements from. predicate A function called as predicate(x), where x is an element of the sequence, and returning a Logical representing whether x should be included in the resulting sequence. In the current version of the library, predicate has to return a Constant Logical.

## Example

namespace hana = boost::hana;
using namespace hana::literals;
hana::take_while(hana::tuple_c<int, 0, 1, 2, 3>, hana::less.than(2_c))
==
hana::tuple_c<int, 0, 1>
);
int main() { }

## ◆ zip

 constexpr auto boost::hana::zip
constexpr

#include <boost/hana/fwd/zip.hpp>

Initial value:
= [](auto&& x1, ..., auto&& xn) {
return tag-dispatched;
}

Zip one sequence or more.

Given n sequences s1, ..., sn, zip produces a sequence whose i-th element is a tuple of (s1[i], ..., sn[i]), where sk[i] denotes the i-th element of the k-th sequence. In other words, zip produces a sequence of the form

[
make_tuple(s1[0], ..., sn[0]),
make_tuple(s1[1], ..., sn[1]),
...
make_tuple(s1[M], ..., sn[M])
]

where M is the length of the sequences, which are all assumed to have the same length. Assuming the sequences to all have the same size allows the library to perform some optimizations. To zip sequences that may have different lengths, zip_shortest should be used instead. Also note that it is an error to provide no sequence at all, i.e. zip expects at least one sequence.

## Example

namespace hana = boost::hana;
static_assert(
hana::zip(hana::make_tuple(1, 'a'), hana::make_tuple(2, 3.3))
==
hana::make_tuple(hana::make_tuple(1, 2), hana::make_tuple('a', 3.3))
, "");
int main() { }

## ◆ zip_shortest

 constexpr auto boost::hana::zip_shortest
constexpr

#include <boost/hana/fwd/zip_shortest.hpp>

Initial value:
= [](auto&& x1, ..., auto&& xn) {
return tag-dispatched;
}

Zip one sequence or more.

Given n sequences s1, ..., sn, zip_shortest produces a sequence whose i-th element is a tuple of (s1[i], ..., sn[i]), where sk[i] denotes the i-th element of the k-th sequence. In other words, zip_shortest produces a sequence of the form

[
make_tuple(s1[0], ..., sn[0]),
make_tuple(s1[1], ..., sn[1]),
...
make_tuple(s1[M], ..., sn[M])
]

where M is the length of the shortest sequence. Hence, the returned sequence stops when the shortest input sequence is exhausted. If you know that all the sequences you are about to zip have the same length, you should use zip instead, since it can be more optimized. Also note that it is an error to provide no sequence at all, i.e. zip_shortest expects at least one sequence.

## Example

namespace hana = boost::hana;
static_assert(
hana::zip_shortest(hana::make_tuple(1, 'a'), hana::make_tuple(2, 3.3), hana::make_tuple(3, 'c', "ignored"))
==
hana::make_tuple(hana::make_tuple(1, 2, 3), hana::make_tuple('a', 3.3, 'c'))
, "");
int main() { }

## ◆ zip_shortest_with

 constexpr auto boost::hana::zip_shortest_with
constexpr

#include <boost/hana/fwd/zip_shortest_with.hpp>

Initial value:
= [](auto&& f, auto&& x1, ..., auto&& xn) {
return tag-dispatched;
}

Zip one sequence or more with a given function.

Given a n-ary function f and n sequences s1, ..., sn, zip_shortest_with produces a sequence whose i-th element is f(s1[i], ..., sn[i]), where sk[i] denotes the i-th element of the k-th sequence. In other words, zip_shortest_with produces a sequence of the form

[
f(s1[0], ..., sn[0]),
f(s1[1], ..., sn[1]),
...
f(s1[M], ..., sn[M])
]

where M is the length of the shortest sequence. Hence, the returned sequence stops when the shortest input sequence is exhausted. If you know that all the sequences you are about to zip have the same length, you should use zip_with instead, since it can be more optimized. Also note that it is an error to provide no sequence at all, i.e. zip_shortest_with expects at least one sequence.

## Example

namespace hana = boost::hana;
static_assert(
hana::zip_shortest_with(hana::mult, hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8, "ignored"))
==
hana::make_tuple(5, 12, 21, 32)
, "");
int main() { }

## ◆ zip_with

 constexpr auto boost::hana::zip_with
constexpr

#include <boost/hana/fwd/zip_with.hpp>

Initial value:
= [](auto&& f, auto&& x1, ..., auto&& xn) {
return tag-dispatched;
}

Zip one sequence or more with a given function.

Given a n-ary function f and n sequences s1, ..., sn, zip_with produces a sequence whose i-th element is f(s1[i], ..., sn[i]), where sk[i] denotes the i-th element of the k-th sequence. In other words, zip_with produces a sequence of the form

[
f(s1[0], ..., sn[0]),
f(s1[1], ..., sn[1]),
...
f(s1[M], ..., sn[M])
]

where M is the length of the sequences, which are all assumed to have the same length. Assuming the sequences to all have the same size allows the library to perform some optimizations. To zip sequences that may have different lengths, zip_shortest_with should be used instead. Also note that it is an error to provide no sequence at all, i.e. zip_with expects at least one sequence.

## Example

#include <tuple>
#include <type_traits>
#include <utility>
namespace hana = boost::hana;
// Basic usage:
static_assert(
hana::zip_with(hana::mult, hana::make_tuple(1, 2, 3, 4), hana::make_tuple(5, 6, 7, 8))
==
hana::make_tuple(5, 12, 21, 32)
, "");
// Example of computing a tuple of all the common types of several tuples:
template<typename... Ts>
using common_tuple_t = typename decltype(
hana::metafunction<std::common_type>,
hana::transform(std::declval<Ts>(), hana::decltype_)...
),
hana::template_<std::tuple>
)
)::type;
static_assert(std::is_same<
common_tuple_t<
>,
>::value, "");
int main() { }
boost::hana::scan_right
constexpr auto scan_right
Fold a Sequence to the right and return a list containing the successive reduction states.
Definition: scan_right.hpp:86
filter.hpp
Defines boost::hana::filter.
less.hpp
Defines boost::hana::less.
equal.hpp
Defines boost::hana::equal.
chain.hpp
Defines boost::hana::chain.
boost::hana::is_empty
constexpr auto is_empty
Returns whether the iterable is empty.
Definition: is_empty.hpp:33
zip.hpp
Defines boost::hana::zip.
find_if.hpp
Defines boost::hana::find_if.
drop_front.hpp
Defines boost::hana::drop_front.
flatten.hpp
Defines boost::hana::flatten.
mod.hpp
Defines boost::hana::mod.
boost::hana::less
constexpr auto less
Returns a Logical representing whether x is less than y.
Definition: less.hpp:37
BOOST_HANA_CONSTANT_CHECK
#define BOOST_HANA_CONSTANT_CHECK(...)
Equivalent to BOOST_HANA_CONSTANT_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERTI...
Definition: assert.hpp:239
ap.hpp
Defines boost::hana::ap.
config.hpp
Defines configuration macros used throughout the library.
drop_back.hpp
Defines boost::hana::drop_back.
tuple.hpp
Defines boost::hana::tuple.
boost::hana::zip_shortest_with
constexpr auto zip_shortest_with
Zip one sequence or more with a given function.
Definition: zip_shortest_with.hpp:46
boost::hana::intersperse
constexpr auto intersperse
Insert a value between each pair of elements in a finite sequence.
Definition: intersperse.hpp:41
is_a.hpp
Defines boost::hana::is_a and boost::hana::is_an.
flip.hpp
Defines boost::hana::flip.
take_while.hpp
Defines boost::hana::take_while.
integral_constant.hpp
Defines boost::hana::integral_constant.
boost::hana::find_if
constexpr auto find_if
Finds the value associated to the first key satisfying a predicate.
Definition: find_if.hpp:41
zip_with.hpp
Defines boost::hana::zip_with.
zip_shortest_with.hpp
Defines boost::hana::zip_shortest_with.
boost::hana::concat
constexpr auto concat
Definition: concat.hpp:47
boost::hana::front
constexpr auto front
Returns the first element of a non-empty iterable.
Definition: front.hpp:32
reverse.hpp
Defines boost::hana::reverse.
boost::hana::insert
constexpr insert_t insert
Insert a value at a given index in a sequence.
Definition: insert.hpp:29
boost::hana::append
constexpr auto append
Append an element to a monadic structure.
Definition: append.hpp:52
boost::hana::take_while
constexpr auto take_while
Take elements from a sequence while the predicate is satisfied.
Definition: take_while.hpp:40
boost::hana::value
constexpr auto value
Return the compile-time value associated to a constant.
Definition: value.hpp:54
boost::hana::zip_with
constexpr auto zip_with
Zip one sequence or more with a given function.
Definition: zip_with.hpp:46
take_front.hpp
Defines boost::hana::take_front and boost::hana::take_front_c.
insert_range.hpp
Defines boost::hana::insert_range.
permutations.hpp
Defines boost::hana::permutations.
boost::hana::take_back
constexpr auto take_back
Returns the last n elements of a sequence, or the whole sequence if the sequence has less than n elem...
Definition: take_back.hpp:42
all_of.hpp
Defines boost::hana::all_of.
boost::hana::length
constexpr auto length
Return the number of elements in a foldable structure.
Definition: length.hpp:34
slice.hpp
Defines boost::hana::slice and boost::hana::slice_c.
boost::hana::take_front
constexpr auto take_front
Returns the first n elements of a sequence, or the whole sequence if the sequence has less than n ele...
Definition: take_front.hpp:42
boost::hana::zip
constexpr auto zip
Zip one sequence or more.
Definition: zip.hpp:45
contains.hpp
Defines boost::hana::contains and boost::hana::in.
range.hpp
Defines boost::hana::range.
boost::hana::for_each
constexpr auto for_each
Perform an action on each element of a foldable, discarding the result each time.
Definition: for_each.hpp:39
type.hpp
Defines boost::hana::type and related utilities.
BOOST_HANA_CONSTEXPR_CHECK
#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
concat.hpp
Defines boost::hana::concat.
take_back.hpp
Defines boost::hana::take_back.
boost::hana::flip
constexpr auto flip
Invoke a function with its two first arguments reversed.
Definition: flip.hpp:31
boost::hana::scan_left
constexpr auto scan_left
Fold a Sequence to the left and return a list containing the successive reduction states.
Definition: scan_left.hpp:86
lift.hpp
Defines boost::hana::lift.
boost::hana
Namespace containing everything in the library.
Definition: accessors.hpp:20
boost::hana::to
constexpr auto to
Converts an object from one data type to another.
Definition: to.hpp:97
remove_at.hpp
Defines boost::hana::remove_at and boost::hana::remove_at_c.
boost::hana::unpack
constexpr auto unpack
Invoke a function with the elements of a Foldable as arguments.
Definition: unpack.hpp:79
boost::hana::mult
constexpr auto mult
Associative operation of a Ring.
Definition: mult.hpp:47
boost::hana::in
constexpr auto in
Return whether the key occurs in the structure.
Definition: contains.hpp:70
assert.hpp
Defines macros to perform different kinds of assertions.
scan_left.hpp
Defines boost::hana::scan_left.
fold_left.hpp
Defines boost::hana::fold_left.
remove_range.hpp
Defines boost::hana::remove_range and boost::hana::remove_range_c.
mult.hpp
Defines boost::hana::mult.
boost::hana::permutations
constexpr auto permutations
Return a sequence of all the permutations of the given sequence.
Definition: permutations.hpp:34
to.hpp
Defines boost::hana::to and related utilities.
boost::hana::remove_at
constexpr auto remove_at
Remove the element at a given index from a sequence.
Definition: remove_at.hpp:46
boost::hana::drop_front
constexpr auto drop_front
Drop the first n elements of an iterable, and return the rest.
Definition: drop_front.hpp:47
boost::hana::reverse
constexpr auto reverse
Reverse a sequence.
Definition: reverse.hpp:33
not_equal.hpp
Defines boost::hana::not_equal.
std::tuple
Definition: tuple.hpp:49
transform.hpp
Defines boost::hana::transform.
empty.hpp
Defines boost::hana::empty.
BOOST_HANA_RUNTIME_CHECK
#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
boost::hana::insert_range
constexpr auto insert_range
Insert several values at a given index in a sequence.
Definition: insert_range.hpp:41
boost::hana::all_of
constexpr auto all_of
Returns whether all the keys of the structure satisfy the predicate.
Definition: all_of.hpp:38
boost::hana::contains
constexpr auto contains
Returns whether the key occurs in the structure.
Definition: contains.hpp:42
is_empty.hpp
Defines boost::hana::is_empty.
front.hpp
Defines boost::hana::front.
length.hpp
Defines boost::hana::length.
zip_shortest.hpp
Defines boost::hana::zip_shortest.
intersperse.hpp
Defines boost::hana::intersperse.
boost::hana::zip_shortest
constexpr auto zip_shortest
Zip one sequence or more.
Definition: zip_shortest.hpp:45
boost::hana::slice
constexpr auto slice
Extract the elements of a Sequence at the given indices.
Definition: slice.hpp:53
tuple.hpp
Adapts std::tuple for use with Hana.
for_each.hpp
Defines boost::hana::for_each.
insert.hpp
Defines boost::hana::insert.
traits.hpp
Defines function-like equivalents to the standard <type_traits>, and also to some utilities like std:...
boost::hana::drop_back
constexpr auto drop_back
Drop the last n elements of a finite sequence, and return the rest.
Definition: drop_back.hpp:44
boost::hana::filter
constexpr auto filter
Filter a monadic structure using a custom predicate.
Definition: filter.hpp:65
pair.hpp
Defines boost::hana::pair.
curry.hpp
Defines boost::hana::curry.
scan_right.hpp
Defines boost::hana::scan_right.
append.hpp
Defines boost::hana::append.
boost::hana::remove_range
constexpr auto remove_range
Remove the elements inside a given range of indices from a sequence.
Definition: remove_range.hpp:49
boost::hana::equal
constexpr auto equal
Returns a Logical representing whether x is equal to y.
Definition: equal.hpp:64
unpack.hpp
Defines boost::hana::unpack.
optional.hpp
Defines boost::hana::optional.