monadic_compose.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // Copyright Louis Dionne 2013-2017
  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. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/config.hpp>
  6. #include <boost/hana/contains.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/if.hpp>
  9. #include <boost/hana/monadic_compose.hpp>
  10. #include <boost/hana/optional.hpp>
  11. #include <boost/hana/tuple.hpp>
  12. #include <boost/hana/type.hpp>
  13. namespace hana = boost::hana;
  14. int main() {
  15. BOOST_HANA_CONSTEXPR_LAMBDA auto block = [](auto ...types) {
  16. return [=](auto x) {
  17. return hana::if_(hana::contains(hana::make_tuple(types...), hana::typeid_(x)),
  18. hana::nothing,
  19. hana::just(x)
  20. );
  21. };
  22. };
  23. BOOST_HANA_CONSTEXPR_LAMBDA auto f = block(hana::type_c<double>);
  24. BOOST_HANA_CONSTEXPR_LAMBDA auto g = block(hana::type_c<int>);
  25. BOOST_HANA_CONSTEXPR_LAMBDA auto h = hana::monadic_compose(g, f);
  26. BOOST_HANA_CONSTANT_CHECK(h(1) == hana::nothing); // fails inside g; 1 has type int
  27. BOOST_HANA_CONSTANT_CHECK(h(1.2) == hana::nothing); // fails inside f; 1.2 has type double
  28. BOOST_HANA_CONSTEXPR_CHECK(h('x') == hana::just('x')); // ok; 'x' has type char
  29. }