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.
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.
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.
Comparable (definition provided automatically)Sequences are equal if and only if they contain the same number of elements and their elements at any given index are equal. Orderable (definition provided automatically)Sequences are ordered using the traditional lexicographical ordering. 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. Applicative (definition provided automatically)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, Example: Monad (definition provided automatically)flatenning a Sequence takes a sequence of sequences and concatenates them to get a larger sequence. In other words, This acts like a std::tuple_cat function, except it receives a sequence of sequences instead of a variadic pack of sequences to flatten.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.MonadPlus (definition provided automatically)Sequences are models of the MonadPlus concept by considering the empty sequence as the unit of concat, and sequence concatenation as concat. FoldableFoldable for Sequences is uniquely determined by the model of Iterable. IterableIterable 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. Searchable (definition provided automatically)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. Variables | |
| constexpr auto | boost::hana::cartesian_product |
Computes the cartesian product of a sequence of sequences.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. More... | |
| constexpr auto | boost::hana::drop_back |
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. More... | |
| constexpr auto | boost::hana::group |
Group adjacent elements of a sequence that all respect a binary predicate, by default equality.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. More... | |
| constexpr insert_t | boost::hana::insert {} |
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. More... | |
| constexpr auto | boost::hana::insert_range |
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. More... | |
| constexpr auto | boost::hana::intersperse |
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. More... | |
| constexpr auto | boost::hana::partition |
Partition a sequence based on a predicate.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. More... | |
| constexpr auto | boost::hana::permutations |
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! More... | |
| constexpr auto | boost::hana::remove_at |
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]. 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.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]. 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.Specifically, reverse(xs) is a new sequence containing the same elements as xs, except in reverse order. More... | |
| constexpr auto | boost::hana::scan_left |
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. More... | |
| constexpr auto | boost::hana::scan_right |
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. More... | |
| constexpr auto | boost::hana::slice |
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,. More... | |
| template<std::size_t from, std::size_t to> | |
| constexpr auto | boost::hana::slice_c |
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. More... | |
| constexpr auto | boost::hana::sort |
Sort a sequence, optionally based on a custom predicate.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. More... | |
| constexpr auto | boost::hana::span |
Returns a Product containing the longest prefix of a sequence satisfying a predicate, and the rest of the sequence.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,. 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.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. 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.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. 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.Specifically, take_while returns a new sequence containing the longest prefix of xs in which all the elements satisfy the given predicate. More... | |
| template<typename S > | |
| constexpr auto | boost::hana::unfold_left |
Dual operation to fold_left for sequences.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. More... | |
| template<typename S > | |
| constexpr auto | boost::hana::unfold_right |
Dual operation to fold_right for sequences.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. More... | |
| constexpr auto | boost::hana::unique |
Removes all consecutive duplicate elements from a Sequence.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. More... | |
| constexpr auto | boost::hana::zip |
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. More... | |
| constexpr auto | boost::hana::zip_shortest |
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. More... | |
| constexpr auto | boost::hana::zip_shortest_with |
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. More... | |
| constexpr auto | boost::hana::zip_with |
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. More... | |
| constexpr auto boost::hana::cartesian_product |
#include <boost/hana/fwd/cartesian_product.hpp>
Computes the cartesian product of a sequence of sequences.Given a sequence of sequences, cartesian_product returns a new sequence of sequences containing the cartesian product of the original sequences. For this method to finish, a finite number of finite sequences must be provided.
Given a Sequence S(T), the signature is
\[ \mathtt{cartesian\_product} : S(S(T)) \to S(S(T)) \]
| xs | A sequence of sequences of which the cartesian product is computed. |
| constexpr auto boost::hana::drop_back |
#include <boost/hana/fwd/drop_back.hpp>
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.
| 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. |
| constexpr auto boost::hana::group |
#include <boost/hana/fwd/group.hpp>
Group adjacent elements of a sequence that all respect a binary predicate, by default equality.Given a finite Sequence and an optional predicate (by default equal), group returns a sequence of subsequences representing groups of adjacent elements that are "equal" with respect to the predicate. In other words, the groups are such that the predicate is satisfied when it is applied to any two adjacent elements in that group. The sequence returned by group is such that the concatenation of its elements is equal to the original sequence, which is equivalent to saying that the order of the elements is not changed.
If no predicate is provided, adjacent elements in the sequence must all be compile-time Comparable.
Given a Sequence s with tag S(T), an IntegralConstant Bool holding a value of type bool, and a predicate \( pred : T \times T \to Bool \), group has the following signatures. For the variant with a provided predicate,
\[ \mathtt{group} : S(T) \times (T \times T \to Bool) \to S(S(T)) \]
for the variant without a custom predicate, T is required to be Comparable. The signature is then
\[ \mathtt{group} : S(T) \to S(S(T)) \]
| xs | The sequence to split into groups. |
| predicate | A binary function called as predicate(x, y), where x and y are adjacent elements in the sequence, whether both elements should be in the same group (subsequence) of the result. In the current version of the library, the result returned by predicate must be an IntegralConstant holding a value of a type convertible to bool. Also, predicate has to define an equivalence relation as defined by the Comparable concept. When this predicate is not provided, it defaults to equal, which requires the comparison of any two adjacent elements in the sequence to return a boolean IntegralConstant. |
group.by) group can be called in a third way, which provides a nice syntax especially when working with the comparing combinator:
where group(-, predicate) denotes the partial application of group to predicate.
|
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.
| 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. |
| constexpr auto boost::hana::insert_range |
#include <boost/hana/fwd/insert_range.hpp>
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.
| 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. |
| constexpr auto boost::hana::intersperse |
#include <boost/hana/fwd/intersperse.hpp>
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.
| xs | The sequence in which a value is interspersed. |
| z | The value to be inserted between every pair of elements of the sequence. |
| constexpr auto boost::hana::partition |
#include <boost/hana/fwd/partition.hpp>
Partition a sequence based on a predicate.Specifically, returns an unspecified Product whose first element is a sequence of the elements satisfying the predicate, and whose second element is a sequence of the elements that do not satisfy the predicate.
Given a Sequence S(T), an IntegralConstant Bool holding a value of type bool, and a predicate \( T \to Bool \), partition has the following signature:
\[ \mathtt{partition} : S(T) \times (T \to Bool) \to S(T) \times S(T) \]
| xs | The sequence to be partitioned. |
| predicate | A function called as predicate(x) for each element x in the sequence, and returning whether x should be added to the sequence in the first component or in the second component of the resulting pair. In the current version of the library, predicate must return an IntegralConstant holding a value convertible to bool. |
partition.by) partition can be called in an alternate way, which provides a nice syntax in some cases where the predicate is short:
where partition(-, predicate) denotes the partial application of partition to predicate.
| constexpr auto boost::hana::permutations |
#include <boost/hana/fwd/permutations.hpp>
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!
| constexpr auto boost::hana::remove_at |
#include <boost/hana/fwd/remove_at.hpp>
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].
| 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. |
| constexpr auto boost::hana::remove_at_c |
#include <boost/hana/fwd/remove_at.hpp>
Equivalent to remove_at; provided for convenience.
| constexpr auto boost::hana::remove_range |
#include <boost/hana/fwd/remove_range.hpp>
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].
| 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. |
| constexpr auto boost::hana::remove_range_c |
#include <boost/hana/fwd/remove_range.hpp>
Equivalent to remove_range; provided for convenience.
| constexpr auto boost::hana::reverse |
#include <boost/hana/fwd/reverse.hpp>
Reverse a sequence.Specifically, reverse(xs) is a new sequence containing the same elements as xs, except in reverse order.
| xs | The sequence to reverse. |
| constexpr auto boost::hana::scan_left |
#include <boost/hana/fwd/scan_left.hpp>
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:
The analogous sequence generated with scan_left will be
Similarly, consider this left fold (without an initial state) on a short sequence:
The analogous sequence generated with scan_left will be
| 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. |
| constexpr auto boost::hana::scan_right |
#include <boost/hana/fwd/scan_right.hpp>
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:
The analogous sequence generated with scan_right will be
Similarly, consider this right fold (without an initial state) on a short sequence:
The analogous sequence generated with scan_right will be
| 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. |
| constexpr auto boost::hana::slice |
#include <boost/hana/fwd/slice.hpp>
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,.
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.
| 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. |
| constexpr auto boost::hana::slice_c |
#include <boost/hana/fwd/slice.hpp>
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.
| 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. |
| constexpr auto boost::hana::sort |
#include <boost/hana/fwd/sort.hpp>
Sort a sequence, optionally based on a custom predicate.Given a Sequence and an optional predicate (by default less), sort returns a new sequence containing the same elements as the original, except they are ordered in such a way that if x comes before y in the sequence, then either predicate(x, y) is true, or both predicate(x, y) and predicate(y, x) are false.
Also note that the sort is guaranteed to be stable. Hence, if x comes before y in the original sequence and both predicate(x, y) and predicate(y, x) are false, then x will come before y in the resulting sequence.
If no predicate is provided, the elements in the sequence must all be compile-time Orderable.
Given a Sequence S(T), a boolean IntegralConstant Bool and a binary predicate \( T \times T \to Bool \), sort has the following signatures. For the variant with a provided predicate,
\[ \mathtt{sort} : S(T) \times (T \times T \to Bool) \to S(T) \]
for the variant without a custom predicate, T is required to be Orderable. The signature is then
\[ \mathtt{sort} : S(T) \to S(T) \]
| xs | The sequence to sort. |
| predicate | A function called as predicate(x, y) for two elements x and y of the sequence, and returning a boolean IntegralConstant representing whether x is to be considered less than y, i.e. whether x should appear before y in the resulting sequence. More specifically, predicate must define a strict weak ordering on the elements of the sequence. When the predicate is not specified, this defaults to less. In the current version of the library, the predicate has to return an IntegralConstant holding a value convertible to a bool. |
sort.by) sort can be called in a third way, which provides a nice syntax especially when working with the ordering combinator:
where sort(-, predicate) denotes the partial application of sort to predicate.
| constexpr auto boost::hana::span |
#include <boost/hana/fwd/span.hpp>
Returns a Product containing the longest prefix of a sequence satisfying a predicate, and the rest of the sequence.The first component of the returned Product is a sequence for which all elements satisfy the given predicate. The second component of the returned Product is a sequence containing the remainder of the argument. Both or either sequences may be empty, depending on the input argument. More specifically,.
except that make_pair may be an arbitrary Product.
Given a Sequence S(T), a Logical Bool and a predicate \( T \to Bool \), span has the following signature:
\[ \mathtt{span} : S(T) \times (T \to Bool) \to S(T) \times S(T) \]
| xs | The sequence to break into two parts. |
| predicate | A function called as predicate(x), where x is an element of the sequence, and returning a Logical. In the current implementation of the library,predicatehas to return a compile-timeLogical`. |
span.by) span can be called in an alternate way, which provides a nice syntax in some cases where the predicate is short:
where span(-, predicate) denotes the partial application of span to predicate.
| constexpr auto boost::hana::take_back |
#include <boost/hana/fwd/take_back.hpp>
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.
| xs | The sequence to take the elements from. |
| n | A non-negative IntegralConstant representing the number of elements to keep in the resulting sequence. |
| constexpr auto boost::hana::take_back_c |
#include <boost/hana/fwd/take_back.hpp>
Equivalent to take_back; provided for convenience.
| constexpr auto boost::hana::take_front |
#include <boost/hana/fwd/take_front.hpp>
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.
| xs | The sequence to take the elements from. |
| n | A non-negative IntegralConstant representing the number of elements to keep in the resulting sequence. |
| constexpr auto boost::hana::take_front_c |
#include <boost/hana/fwd/take_front.hpp>
Equivalent to take_front; provided for convenience.
| constexpr auto boost::hana::take_while |
#include <boost/hana/fwd/take_while.hpp>
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.
| 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. |
| constexpr auto boost::hana::unfold_left |
#include <boost/hana/fwd/unfold_left.hpp>
Dual operation to fold_left for sequences.While fold_left reduces a structure to a summary value from the left, unfold_left builds a sequence from a seed value and a function, starting from the left.
Given a Sequence S, an initial value state of tag I, an arbitrary Product P and a function \( f : I \to P(I, T) \), unfold_left<S> has the following signature:
\[ \mathtt{unfold\_left}_S : I \times (I \to P(I, T)) \to S(T) \]
| S | The tag of the sequence to build up. |
| state | An initial value to build the sequence from. |
| f | A function called as f(state), where state is an initial value, and returning
|
In some cases, unfold_left can undo a fold_left operation:
if the following holds
| constexpr auto boost::hana::unfold_right |
#include <boost/hana/fwd/unfold_right.hpp>
Dual operation to fold_right for sequences.While fold_right reduces a structure to a summary value from the right, unfold_right builds a sequence from a seed value and a function, starting from the right.
Given a Sequence S, an initial value state of tag I, an arbitrary Product P and a function \( f : I \to P(T, I) \), unfold_right<S> has the following signature:
\[ \mathtt{unfold\_right}_S : I \times (I \to P(T, I)) \to S(T) \]
| S | The tag of the sequence to build up. |
| state | An initial value to build the sequence from. |
| f | A function called as f(state), where state is an initial value, and returning
|
In some cases, unfold_right can undo a fold_right operation:
if the following holds
| constexpr auto boost::hana::unique |
#include <boost/hana/fwd/unique.hpp>
Removes all consecutive duplicate elements from a Sequence.Given a Sequence and an optional binary predicate, unique returns a new sequence containing only the first element of every subrange of the original sequence whose elements are all equal. In other words, it turns a sequence of the form [a, a, b, c, c, c, d, d, d, a] into a sequence [a, b, c, d, a]. The equality of two elements is determined by the provided predicate, or by equal if no predicate is provided.
Given a Sequence S(T), a Logical Bool and a binary predicate \( T \times T \to Bool \), unique has the following signature:
\[ \mathtt{unique} : S(T) \times (T \times T \to Bool) \to S(T) \]
| xs | The sequence from which to remove consecutive duplicates. |
| predicate | A function called as predicate(x, y), where x and y are adjacent elements of the sequence, and returning a Logical representing whether x and y should be considered equal. predicate should define an equivalence relation over the elements of the sequence. In the current implementation of the library, predicate has to return a compile-time Logical. This parameter is optional; it defaults to equal if it is not provided, which then requires the elements of the sequence to be compile-time Comparable. |
unique.by) unique can be called in an alternate way, which provides a nice syntax, especially in conjunction with the comparing combinator:
where unique(-, predicate) denotes the partial application of unique to predicate.
| constexpr auto boost::hana::zip |
#include <boost/hana/fwd/zip.hpp>
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.
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.
| constexpr auto boost::hana::zip_shortest |
#include <boost/hana/fwd/zip_shortest.hpp>
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.
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.
| constexpr auto boost::hana::zip_shortest_with |
#include <boost/hana/fwd/zip_shortest_with.hpp>
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.
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.
| constexpr auto boost::hana::zip_with |
#include <boost/hana/fwd/zip_with.hpp>
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.
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.