/* @copyright Louis Dionne 2014 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 #include #include #include #include #include #include #include #include #include #include #include #include using namespace boost::hana; int main() { { //! [comparable] BOOST_HANA_CONSTEXPR_CHECK(make(1, 2, 3) == make(1, 2, 3)); BOOST_HANA_CONSTEXPR_CHECK(make(1, 2, 3) != make(1, 2, 3, 4)); //! [comparable] }{ //! [orderable] BOOST_HANA_CONSTEXPR_CHECK(make(1, 2, 3) < make(2, 3, 4)); BOOST_HANA_CONSTEXPR_CHECK(make(1, 2, 3) < make(1, 2, 3, 4)); //! [orderable] }{ //! [foldable] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; auto show = [=](auto x, auto y) { return "(" + to_string(x) + " + " + to_string(y) + ")"; }; BOOST_HANA_RUNTIME_CHECK(foldl(make(2, "3", '4'), "1", show) == "(((1 + 2) + 3) + 4)"); //! [foldable] }{ //! [iterable] BOOST_HANA_CONSTEXPR_CHECK(head(make(1, '2', 3.3)) == 1); BOOST_HANA_CONSTEXPR_CHECK(tail(make(1, '2', 3.3)) == make('2', 3.3)); BOOST_HANA_CONSTANT_CHECK(!is_empty(make(1, '2', 3.3))); BOOST_HANA_CONSTANT_CHECK(is_empty(make())); //! [iterable] }{ //! [functor] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; BOOST_HANA_RUNTIME_CHECK( transform(make(1, '2', "345", std::string{"67"}), to_string) == make("1", "2", "345", "67") ); //! [functor] }{ //! [applicative] BOOST_HANA_CONSTEXPR_CHECK(lift('x') == make('x')); BOOST_HANA_CONSTEXPR_CHECK(equal(lift('x'), std::make_tuple('x'))); BOOST_HANA_CONSTEXPR_LAMBDA auto f = pair; BOOST_HANA_CONSTEXPR_LAMBDA auto g = flip(pair); BOOST_HANA_CONSTEXPR_CHECK( ap(make(f, g), make(1, 2, 3), make('a', 'b')) == make( 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') ) ); //! [applicative] }{ //! [monad] BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) { return make(x, -x); }; BOOST_HANA_CONSTEXPR_CHECK((make(1, 2, 3) | f) == make(1, -1, 2, -2, 3, -3)); BOOST_HANA_CONSTEXPR_CHECK( flatten(make(make(1, 2), make(3, 4), make(make(5, 6)))) == make(1, 2, 3, 4, make(5, 6)) ); //! [monad] }{ //! [traversable] using namespace std::string_literals; BOOST_HANA_RUNTIME_CHECK( sequence( make(make("a1"s, "a2"s), make("b1"s), make("c1"s, "c2"s, "c3"s)) ) == make( make("a1"s, "b1"s, "c1"s), make("a1"s, "b1"s, "c2"s), make("a1"s, "b1"s, "c3"s), make("a2"s, "b1"s, "c1"s), make("a2"s, "b1"s, "c2"s), make("a2"s, "b1"s, "c3"s) ) ); BOOST_HANA_CONSTEXPR_LAMBDA auto half = [](auto x) { return if_(x % int_<2> == int_<0>, just(x / int_<2>), nothing ); }; BOOST_HANA_CONSTANT_CHECK( traverse(make(int_<2>, int_<4>, int_<6>), half) == just(make(int_<1>, int_<2>, int_<3>)) ); BOOST_HANA_CONSTANT_CHECK( traverse(make(int_<2>, int_<3>, int_<6>), half) == nothing ); //! [traversable] }{ //! [make] BOOST_HANA_CONSTANT_CHECK(make() == make()); BOOST_HANA_CONSTEXPR_CHECK(make(1, '2', 3.3) == make(1, '2', 3.3)); //! [make] }{ //! [init] using namespace literals; BOOST_HANA_CONSTANT_CHECK(init(make(1)) == make()); BOOST_HANA_CONSTEXPR_CHECK(init(make(1, '2', 3.3, 4_c)) == make(1, '2', 3.3)); //! [init] }{ //! [intersperse] BOOST_HANA_CONSTEXPR_CHECK( intersperse(make(1, '2', 3.3), 'x') == make(1, 'x', '2', 'x', 3.3) ); BOOST_HANA_CONSTANT_CHECK(intersperse(make(), 'x') == make()); //! [intersperse] }{ //! [remove_at] BOOST_HANA_CONSTEXPR_CHECK( remove_at(int_<2>, make(0, '1', 2.2, 3u)) == make(0, '1', 3u) ); //! [remove_at] }{ //! [remove_at_c] BOOST_HANA_CONSTEXPR_CHECK( remove_at_c<2>(make(0, '1', 2.2, 3u)) == make(0, '1', 3u) ); //! [remove_at_c] }{ //! [reverse] BOOST_HANA_CONSTEXPR_CHECK(reverse(make(1, '2', 3.3)) == make(3.3, '2', 1)); //! [reverse] }{ //! [group_by] BOOST_HANA_CONSTEXPR_CHECK( group_by(equal ^on^ decltype_, make(1, 2, 3, 'x', 'y', 4.4, 5.5) ) == make( make(1, 2, 3), make('x', 'y'), make(4.4, 5.5) ) ); //! [group_by] }{ //! [group] BOOST_HANA_CONSTANT_CHECK( group(make(int_<1>, long_<1>, type, char_<'x'>, char_<'x'>)) == make( make(int_<1>, long_<1>), make(type), make(char_<'x'>, char_<'x'>) ) ); //! [group] }{ //! [zip] BOOST_HANA_CONSTEXPR_CHECK( zip(make(1, 'a'), make(2, 3.3)) == make(make(1, 2), make('a', 3.3)) ); BOOST_HANA_CONSTEXPR_CHECK( zip(make(1, 'a'), make(2, 3.3), make(3, 'c', "ignored")) == make(make(1, 2, 3), make('a', 3.3, 'c')) ); //! [zip] }{ //! [zip_with] BOOST_HANA_CONSTEXPR_CHECK( zip.with(_ * _, make(1, 2, 3, 4), make(5, 6, 7, 8, "ignored")) == make(5, 12, 21, 32) ); //! [zip_with] }{ //! [unzip] BOOST_HANA_CONSTEXPR_CHECK( unzip(make(make(1, '2', 3.3), make('4', 5.5, 6))) == make(make(1, '4'), make('2', 5.5), make(3.3, 6)) ); BOOST_HANA_CONSTEXPR_CHECK( unzip(make(make(1, '2', 3.3), make('4', 5.5, 6, "ignored"))) == make(make(1, '4'), make('2', 5.5), make(3.3, 6)) ); //! [unzip] }{ //! [take_while] using namespace literals; BOOST_HANA_CONSTANT_CHECK( take_while(tuple_c, _ < 2_c) == tuple_c ); //! [take_while] }{ //! [take_until] using namespace literals; BOOST_HANA_CONSTANT_CHECK( take_until(tuple_c, _ < 2_c) == tuple_c ); //! [take_until] }{ //! [take] using namespace literals; BOOST_HANA_CONSTANT_CHECK(take(0_c, make(1, '2', 3.3)) == make()); BOOST_HANA_CONSTEXPR_CHECK(take(1_c, make(1, '2', 3.3)) == make(1)); BOOST_HANA_CONSTEXPR_CHECK(take(2_c, make(1, '2', 3.3)) == make(1, '2')); BOOST_HANA_CONSTEXPR_CHECK(take(3_c, make(1, '2', 3.3)) == make(1, '2', 3.3)); BOOST_HANA_CONSTEXPR_CHECK(take(4_c, make(1, '2', 3.3)) == make(1, '2', 3.3)); //! [take] }{ //! [take_c] BOOST_HANA_CONSTEXPR_CHECK(take_c<2>(make(1, '2', 3.3)) == make(1, '2')); //! [take_c] }{ //! [span] BOOST_HANA_CONSTEXPR_LAMBDA auto xs = make(int_<1>, int_<2>, int_<3>, int_<4>); BOOST_HANA_CONSTANT_CHECK( span(xs, _ < int_<3>) == pair(make(int_<1>, int_<2>), make(int_<3>, int_<4>)) ); BOOST_HANA_CONSTANT_CHECK( span(xs, _ < int_<0>) == pair(make(), xs) ); BOOST_HANA_CONSTANT_CHECK( span(xs, _ < int_<5>) == pair(xs, make()) ); //! [span] }{ //! [sort] using namespace literals; BOOST_HANA_CONSTANT_CHECK( sort(make(1_c, -2_c, 3_c, 0_c)) == make(-2_c, 0_c, 1_c, 3_c) ); //! [sort] }{ //! [sort_by] using namespace literals; BOOST_HANA_CONSTANT_CHECK( sort_by(_>_, make(1_c, -2_c, 3_c, 0_c)) == make(3_c, 1_c, 0_c, -2_c) ); //! [sort_by] }{ //! [slice] BOOST_HANA_CONSTEXPR_CHECK( slice(make(1, '2', 3.3, type), int_<1>, int_<3>) == make('2', 3.3) ); //! [slice] }{ //! [slice_c] BOOST_HANA_CONSTEXPR_CHECK( slice_c<1, 3>(make(1, '2', 3.3, type)) == make('2', 3.3) ); //! [slice_c] }{ //! [permutations] BOOST_HANA_CONSTEXPR_LAMBDA auto is_permutation_of = curry<2>([](auto xs, auto perm) { return elem(permutations(xs), perm); }); BOOST_HANA_CONSTEXPR_CHECK( all_of( make( make('1', 2, 3.0), make('1', 3.0, 2), make(2, '1', 3.0), make(2, 3.0, '1'), make(3.0, '1', 2), make(3.0, 2, '1') ), is_permutation_of(make('1', 2, 3.0)) ) ); //! [permutations] }{ //! [unfoldl] BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) { return if_(x == int_<0>, nothing, just(pair(x - int_<1>, x))); }; BOOST_HANA_CONSTANT_CHECK( unfoldl(f, int_<10>) == tuple_c ); //! [unfoldl] }{ //! [unfoldr] BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) { return if_(x == int_<0>, nothing, just(pair(x, x - int_<1>))); }; BOOST_HANA_CONSTANT_CHECK( unfoldr(f, int_<10>) == tuple_c ); //! [unfoldr] }{ //! [scanl] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; auto show = [=](auto x, auto y) { return "(" + to_string(x) + " + " + to_string(y) + ")"; }; BOOST_HANA_RUNTIME_CHECK(scanl(make(2, "3", '4'), 1, show) == make( 1, "(1 + 2)", "((1 + 2) + 3)", "(((1 + 2) + 3) + 4)" )); //! [scanl] }{ //! [scanl1] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; auto show = [=](auto x, auto y) { return "(" + to_string(x) + " + " + to_string(y) + ")"; }; BOOST_HANA_RUNTIME_CHECK(scanl1(make(1, "2", '3'), show) == make( 1, "(1 + 2)", "((1 + 2) + 3)" )); //! [scanl1] }{ //! [scanr] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; auto show = [=](auto x, auto y) { return "(" + to_string(x) + " + " + to_string(y) + ")"; }; BOOST_HANA_RUNTIME_CHECK(scanr(make(1, "2", '3'), 4, show) == make( "(1 + (2 + (3 + 4)))", "(2 + (3 + 4))", "(3 + 4)", 4 )); //! [scanr] }{ //! [scanr1] auto to_string = [](auto x) { return static_cast(std::ostringstream{} << x).str(); }; auto show = [=](auto x, auto y) { return "(" + to_string(x) + " + " + to_string(y) + ")"; }; BOOST_HANA_RUNTIME_CHECK(scanr1(make(1, "2", '3'), show) == make( "(1 + (2 + 3))", "(2 + 3)", '3' )); //! [scanr1] }{ //! [partition] BOOST_HANA_CONSTANT_CHECK( partition(tuple_c, [](auto x) { return x % int_<2> != int_<0>; }) == pair( tuple_c, tuple_c ) ); BOOST_HANA_CONSTANT_CHECK( partition(tuple_t, trait) == pair( tuple_t, tuple_t ) ); //! [partition] } }