2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-01 08:32:11 +00:00
Files
hana/example/monad.cpp
2015-02-11 11:15:16 -05:00

84 lines
2.0 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/bool.hpp>
#include <boost/hana/detail/constexpr.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <sstream>
using namespace boost::hana;
int main() {
{
//! [flatten]
BOOST_HANA_CONSTEXPR_CHECK(
flatten(tuple(tuple(1, 2, 3), tuple(4, 5), tuple(6, 7, 8, 9)))
==
tuple(1, 2, 3, 4, 5, 6, 7, 8, 9)
);
BOOST_HANA_CONSTANT_CHECK(flatten(nothing) == nothing);
BOOST_HANA_CONSTEXPR_CHECK(flatten(just(just(1))) == just(1));
BOOST_HANA_CONSTANT_CHECK(flatten(just(nothing)) == nothing);
//! [flatten]
}{
//! [mcompose]
BOOST_HANA_CONSTEXPR_LAMBDA auto block = [](auto ...types) {
return [=](auto x) {
return if_(elem(tuple(types...), decltype_(x)),
nothing,
just(x)
);
};
};
BOOST_HANA_CONSTEXPR_LAMBDA auto f = block(type<double>);
BOOST_HANA_CONSTEXPR_LAMBDA auto g = block(type<int>);
BOOST_HANA_CONSTEXPR_LAMBDA auto h = mcompose<Maybe>(f, g);
BOOST_HANA_CONSTANT_CHECK(h(1) == nothing); // fails inside g; 1 has type int
BOOST_HANA_CONSTANT_CHECK(h(1.2) == nothing); // fails inside f; 1.2 has type double
BOOST_HANA_CONSTEXPR_CHECK(h('x') == just('x')); // ok; 'x' has type char
//! [mcompose]
}{
//! [then]
struct undefined { };
BOOST_HANA_CONSTEXPR_CHECK(
then(tuple(undefined{}, undefined{}), tuple(1, 2, 3)) == tuple(
1, 2, 3,
1, 2, 3
)
);
//! [then]
}{
//! [tap]
std::stringstream before, after;
auto xs = tuple(1, 2, 3)
| tap<Tuple>([&](auto x) { before << x << ' '; })
| [](auto x) { return tuple(x, -x); }
| tap<Tuple>([&](auto x) { after << x << ' '; });
BOOST_HANA_RUNTIME_CHECK(before.str() == "1 2 3 ");
BOOST_HANA_RUNTIME_CHECK(after.str() == "1 -1 2 -2 3 -3 ");
BOOST_HANA_RUNTIME_CHECK(xs == tuple(1, -1, 2, -2, 3, -3));
//! [tap]
}
}