monadic_fold_right.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/div.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/eval_if.hpp>
  9. #include <boost/hana/integral_constant.hpp>
  10. #include <boost/hana/lazy.hpp>
  11. #include <boost/hana/monadic_fold_right.hpp>
  12. #include <boost/hana/optional.hpp>
  13. #include <boost/hana/tuple.hpp>
  14. namespace hana = boost::hana;
  15. int main() {
  16. BOOST_HANA_CONSTEXPR_LAMBDA auto safe_div = [](auto x, auto y) {
  17. return hana::eval_if(y == hana::int_c<0>,
  18. hana::make_lazy(hana::nothing),
  19. [=](auto _) {
  20. return hana::just(_(x) / y);
  21. }
  22. );
  23. };
  24. // with an initial state
  25. BOOST_HANA_CONSTANT_CHECK(
  26. hana::monadic_fold_right<hana::optional_tag>(
  27. hana::tuple_c<int, 1000, 8, 4>, hana::int_c<2>, safe_div
  28. )
  29. ==
  30. hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
  31. );
  32. BOOST_HANA_CONSTANT_CHECK(
  33. hana::monadic_fold_right<hana::optional_tag>(
  34. hana::tuple_c<int, 1000, 8, 4>, hana::int_c<0>, safe_div
  35. )
  36. ==
  37. hana::nothing
  38. );
  39. // without an initial state
  40. BOOST_HANA_CONSTANT_CHECK(
  41. hana::monadic_fold_right<hana::optional_tag>(
  42. hana::tuple_c<int, 1000, 8, 4, 2>, safe_div
  43. )
  44. ==
  45. hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
  46. );
  47. BOOST_HANA_CONSTANT_CHECK(
  48. hana::monadic_fold_right<hana::optional_tag>(
  49. hana::tuple_c<int, 1000, 8, 4, 0>, safe_div
  50. )
  51. ==
  52. hana::nothing
  53. );
  54. }