3 Defines `boost::hana::monadic_fold_right`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_MONADIC_FOLD_RIGHT_HPP
11 #define BOOST_HANA_MONADIC_FOLD_RIGHT_HPP
13 #include <boost/hana/fwd/monadic_fold_right.hpp>
15 #include <boost/hana/chain.hpp>
16 #include <boost/hana/concept/foldable.hpp>
17 #include <boost/hana/concept/monad.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/detail/decay.hpp>
21 #include <boost/hana/fold_left.hpp>
22 #include <boost/hana/functional/curry.hpp>
23 #include <boost/hana/functional/partial.hpp>
24 #include <boost/hana/lift.hpp>
26 #include <type_traits>
29 BOOST_HANA_NAMESPACE_BEGIN
31 struct monadic_fold_right_t {
32 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33 static_assert(hana::Monad<M>::value,
34 "hana::monadic_fold_right<M> requires 'M' to be a Monad");
37 template <typename Xs, typename State, typename F>
38 constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const {
39 using S = typename hana::tag_of<Xs>::type;
40 using MonadicFoldRight = BOOST_HANA_DISPATCH_IF(monadic_fold_right_impl<S>,
41 hana::Foldable<S>::value
44 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
45 static_assert(hana::Foldable<S>::value,
46 "hana::monadic_fold_right<M>(xs, state, f) requires 'xs' to be Foldable");
49 return MonadicFoldRight::template apply<M>(static_cast<Xs&&>(xs),
50 static_cast<State&&>(state),
54 template <typename Xs, typename F>
55 constexpr decltype(auto) operator()(Xs&& xs, F&& f) const {
56 using S = typename hana::tag_of<Xs>::type;
57 using MonadicFoldRight = BOOST_HANA_DISPATCH_IF(monadic_fold_right_impl<S>,
58 hana::Foldable<S>::value
61 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
62 static_assert(hana::Foldable<S>::value,
63 "hana::monadic_fold_right<M>(xs, f) requires 'xs' to be Foldable");
65 return MonadicFoldRight::template apply<M>(static_cast<Xs&&>(xs),
71 struct foldrM_helper {
72 template <typename F, typename K, typename X, typename Z>
73 constexpr decltype(auto) operator()(F&& f, K&& k, X&& x, Z&& z) const {
84 template <typename End, typename M, typename F>
85 struct monadic_foldr1_helper {
87 template <typename X, typename Y>
88 constexpr decltype(auto) operator()(X&& x, Y&& y) const
89 { return f(static_cast<X&&>(x), static_cast<Y&&>(y)); }
91 constexpr decltype(auto) operator()(X&& x, End) const
92 { return hana::lift<M>(static_cast<X&&>(x)); }
96 template <typename T, bool condition>
97 struct monadic_fold_right_impl<T, when<condition>> : default_ {
99 template <typename M, typename Xs, typename S, typename F>
100 static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
101 return hana::fold_left(
102 static_cast<Xs&&>(xs),
104 hana::curry<3>(hana::partial(
105 detail::foldrM_helper{}, static_cast<F&&>(f)
107 )(static_cast<S&&>(s));
111 template <typename M, typename Xs, typename F>
112 static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
114 using G = detail::monadic_foldr1_helper<end, M, typename detail::decay<F>::type>;
115 decltype(auto) result = hana::monadic_fold_right<M>(
116 static_cast<Xs&&>(xs),
118 G{static_cast<F&&>(f)}
121 static_assert(!std::is_same<
122 std::remove_reference_t<decltype(result)>,
123 decltype(hana::lift<M>(end{}))
125 "hana::monadic_fold_right<M>(xs, f) requires 'xs' to be non-empty");
129 BOOST_HANA_NAMESPACE_END
131 #endif // !BOOST_HANA_MONADIC_FOLD_RIGHT_HPP