| 1 | // Copyright Louis Dionne 2013-2022 |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/assert.hpp> |
| 6 | #include <boost/hana/core/to.hpp> |
| 7 | #include <boost/hana/equal.hpp> |
| 8 | #include <boost/hana/filter.hpp> |
| 9 | #include <boost/hana/integral_constant.hpp> |
| 10 | #include <boost/hana/length.hpp> |
| 11 | #include <boost/hana/mod.hpp> |
| 12 | #include <boost/hana/range.hpp> |
| 13 | #include <boost/hana/slice.hpp> |
| 14 | #include <boost/hana/tuple.hpp> |
| 15 | #include <boost/hana/type.hpp> |
| 16 | namespace hana = boost::hana; |
| 17 | using namespace hana::literals; |
| 18 | |
| 19 | |
| 20 | // Slice a contiguous range |
| 21 | constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3_c, hana::type_c<float>); |
| 22 | |
| 23 | static_assert( |
| 24 | hana::slice(xs, hana::tuple_c<std::size_t, 1, 2, 3>) == |
| 25 | hana::make_tuple('1', 2.2, 3_c) |
| 26 | , "" ); |
| 27 | |
| 28 | |
| 29 | // A more complex example with a non-contiguous range |
| 30 | constexpr auto letters = hana::to_tuple(hana::range_c<char, 'a', 'z'>); |
| 31 | constexpr auto indices = hana::to_tuple(hana::make_range(hana::size_c<0>, hana::length(letters))); |
| 32 | |
| 33 | auto even_indices = hana::filter(indices, [](auto n) { |
| 34 | return n % hana::size_c<2> == hana::size_c<0>; |
| 35 | }); |
| 36 | |
| 37 | BOOST_HANA_CONSTANT_CHECK( |
| 38 | hana::slice(letters, even_indices) == hana::tuple_c<char, |
| 39 | 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y' |
| 40 | > |
| 41 | ); |
| 42 | |
| 43 | int main() { } |
| 44 | |