3 Defines `boost::hana::monadic_fold_left`.
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_LEFT_HPP
11 #define BOOST_HANA_MONADIC_FOLD_LEFT_HPP
13 #include <boost/hana/fwd/monadic_fold_left.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_right.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_left_t {
32 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33 static_assert(hana::Monad<M>::value,
34 "hana::monadic_fold_left<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 MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_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_left<M>(xs, state, f) requires 'xs' to be Foldable");
49 return MonadicFoldLeft::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 MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_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_left<M>(xs, f) requires 'xs' to be Foldable");
66 return MonadicFoldLeft::template apply<M>(static_cast<Xs&&>(xs),
72 struct foldlM_helper {
73 template <typename F, typename X, typename K, typename Z>
74 constexpr decltype(auto) operator()(F&& f, X&& x, K&& k, Z&& z) const {
85 template <typename End, typename M, typename F>
86 struct monadic_foldl1_helper {
88 template <typename X, typename Y>
89 constexpr decltype(auto) operator()(X&& x, Y&& y) const
90 { return f(static_cast<X&&>(x), static_cast<Y&&>(y)); }
92 constexpr decltype(auto) operator()(End, Y&& y) const
93 { return hana::lift<M>(static_cast<Y&&>(y)); }
97 template <typename T, bool condition>
98 struct monadic_fold_left_impl<T, when<condition>> : default_ {
100 template <typename M, typename Xs, typename S, typename F>
101 static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
102 return hana::fold_right(
103 static_cast<Xs&&>(xs),
105 hana::curry<3>(hana::partial(
106 detail::foldlM_helper{}, static_cast<F&&>(f)
108 )(static_cast<S&&>(s));
112 template <typename M, typename Xs, typename F>
113 static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
115 using G = detail::monadic_foldl1_helper<end, M, typename detail::decay<F>::type>;
116 decltype(auto) result = hana::monadic_fold_left<M>(
117 static_cast<Xs&&>(xs),
119 G{static_cast<F&&>(f)}
122 static_assert(!std::is_same<
123 std::remove_reference_t<decltype(result)>,
124 decltype(hana::lift<M>(end{}))
126 "hana::monadic_fold_left<M>(xs, f) requires 'xs' to be non-empty");
130 BOOST_HANA_NAMESPACE_END
132 #endif // !BOOST_HANA_MONADIC_FOLD_LEFT_HPP