-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathmonadic_fold_right.cpp
61 lines (54 loc) · 1.81 KB
/
monadic_fold_right.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Louis Dionne 2013-2022
// 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/config.hpp>
#include <boost/hana/div.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/eval_if.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/lazy.hpp>
#include <boost/hana/monadic_fold_right.hpp>
#include <boost/hana/optional.hpp>
#include <boost/hana/tuple.hpp>
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTEXPR_LAMBDA auto safe_div = [](auto x, auto y) {
return hana::eval_if(y == hana::int_c<0>,
hana::make_lazy(hana::nothing),
[=](auto _) {
return hana::just(_(x) / y);
}
);
};
// with an initial state
BOOST_HANA_CONSTANT_CHECK(
hana::monadic_fold_right<hana::optional_tag>(
hana::tuple_c<int, 1000, 8, 4>, hana::int_c<2>, safe_div
)
==
hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
);
BOOST_HANA_CONSTANT_CHECK(
hana::monadic_fold_right<hana::optional_tag>(
hana::tuple_c<int, 1000, 8, 4>, hana::int_c<0>, safe_div
)
==
hana::nothing
);
// without an initial state
BOOST_HANA_CONSTANT_CHECK(
hana::monadic_fold_right<hana::optional_tag>(
hana::tuple_c<int, 1000, 8, 4, 2>, safe_div
)
==
hana::just(hana::int_c<1000> / (hana::int_c<8> / (hana::int_c<4> / hana::int_c<2>)))
);
BOOST_HANA_CONSTANT_CHECK(
hana::monadic_fold_right<hana::optional_tag>(
hana::tuple_c<int, 1000, 8, 4, 0>, safe_div
)
==
hana::nothing
);
}