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)
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/config.hpp>
7 #include <boost/hana/div.hpp>
8 #include <boost/hana/equal.hpp>
9 #include <boost/hana/eval_if.hpp>
10 #include <boost/hana/integral_constant.hpp>
11 #include <boost/hana/lazy.hpp>
12 #include <boost/hana/monadic_fold_right.hpp>
13 #include <boost/hana/optional.hpp>
14 #include <boost/hana/tuple.hpp>
15 namespace hana
= boost::hana
;
19 BOOST_HANA_CONSTEXPR_LAMBDA
auto safe_div
= [](auto x
, auto y
) {
20 return hana::eval_if(y
== hana::int_c
<0>,
21 hana::make_lazy(hana::nothing
),
23 return hana::just(_(x
) / y
);
28 // with an initial state
29 BOOST_HANA_CONSTANT_CHECK(
30 hana::monadic_fold_right
<hana::optional_tag
>(
31 hana::tuple_c
<int, 1000, 8, 4>, hana::int_c
<2>, safe_div
34 hana::just(hana::int_c
<1000> / (hana::int_c
<8> / (hana::int_c
<4> / hana::int_c
<2>)))
37 BOOST_HANA_CONSTANT_CHECK(
38 hana::monadic_fold_right
<hana::optional_tag
>(
39 hana::tuple_c
<int, 1000, 8, 4>, hana::int_c
<0>, safe_div
45 // without an initial state
46 BOOST_HANA_CONSTANT_CHECK(
47 hana::monadic_fold_right
<hana::optional_tag
>(
48 hana::tuple_c
<int, 1000, 8, 4, 2>, safe_div
51 hana::just(hana::int_c
<1000> / (hana::int_c
<8> / (hana::int_c
<4> / hana::int_c
<2>)))
54 BOOST_HANA_CONSTANT_CHECK(
55 hana::monadic_fold_right
<hana::optional_tag
>(
56 hana::tuple_c
<int, 1000, 8, 4, 0>, safe_div