2
0
mirror of https://github.com/boostorg/hana.git synced 2026-01-22 17:22:30 +00:00
Files
hana/example/traversable.cpp
2015-02-01 21:02:18 -05:00

73 lines
1.9 KiB
C++

/*
@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 <boost/hana/assert.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/tuple.hpp>
using namespace boost::hana;
int main() {
{
//! [sequence]
BOOST_HANA_CONSTEXPR_CHECK(
sequence<Maybe>(tuple(just(1), just('2'), just(3.3))) == just(tuple(1, '2', 3.3))
);
BOOST_HANA_CONSTANT_CHECK(
sequence<Maybe>(tuple(just(1), nothing, just(3.3))) == nothing
);
// This is a generalized Cartesian product.
BOOST_HANA_CONSTEXPR_CHECK(
sequence<Tuple>(tuple(tuple(1, 2, 3), tuple(4), tuple(5, 6)))
==
tuple(
tuple(1, 4, 5), tuple(1, 4, 6),
tuple(2, 4, 5), tuple(2, 4, 6),
tuple(3, 4, 5), tuple(3, 4, 6)
)
);
//! [sequence]
}
{
//! [traverse]
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<Maybe>(tuple(int_<2>, int_<4>, int_<6>), half)
==
just(tuple(int_<1>, int_<2>, int_<3>))
);
BOOST_HANA_CONSTANT_CHECK(
traverse<Maybe>(tuple(int_<2>, int_<3>, int_<6>), half)
==
nothing
);
BOOST_HANA_CONSTEXPR_LAMBDA auto twice = [](auto x) {
return tuple(x, x);
};
BOOST_HANA_CONSTEXPR_CHECK(
traverse<Tuple>(just('x'), twice) == tuple(just('x'), just('x'))
);
BOOST_HANA_CONSTANT_CHECK(
traverse<Tuple>(nothing, twice) == tuple(nothing)
);
//! [traverse]
}
}