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 
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;
16 
17 
main()18 int main() {
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),
22             [=](auto _) {
23                 return hana::just(_(x) / y);
24             }
25         );
26     };
27 
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
32         )
33             ==
34         hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
35     );
36 
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
40         )
41             ==
42         hana::nothing
43     );
44 
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
49         )
50             ==
51         hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
52     );
53 
54     BOOST_HANA_CONSTANT_CHECK(
55         hana::monadic_fold_right<hana::optional_tag>(
56             hana::tuple_c<int, 1000, 8, 4, 0>, safe_div
57         )
58             ==
59         hana::nothing
60     );
61 }
62