3 Defines `boost::hana::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_FOLD_RIGHT_HPP
11 #define BOOST_HANA_FOLD_RIGHT_HPP
13 #include <boost/hana/fwd/fold_right.hpp>
15 #include <boost/hana/concept/foldable.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/detail/variadic/foldr1.hpp>
19 #include <boost/hana/functional/partial.hpp>
20 #include <boost/hana/fwd/unpack.hpp>
23 BOOST_HANA_NAMESPACE_BEGIN
25 template <typename Xs
, typename State
, typename F
>
26 constexpr decltype(auto) fold_right_t::operator()(Xs
&& xs
, State
&& state
, F
&& f
) const {
27 using S
= typename
hana::tag_of
<Xs
>::type
;
28 using FoldRight
= BOOST_HANA_DISPATCH_IF(fold_right_impl
<S
>,
29 hana::Foldable
<S
>::value
32 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33 static_assert(hana::Foldable
<S
>::value
,
34 "hana::fold_right(xs, state, f) requires 'xs' to be Foldable");
37 return FoldRight::apply(static_cast<Xs
&&>(xs
),
38 static_cast<State
&&>(state
),
42 template <typename Xs
, typename F
>
43 constexpr decltype(auto) fold_right_t::operator()(Xs
&& xs
, F
&& f
) const {
44 using S
= typename
hana::tag_of
<Xs
>::type
;
45 using FoldRight
= BOOST_HANA_DISPATCH_IF(fold_right_impl
<S
>,
46 hana::Foldable
<S
>::value
49 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
50 static_assert(hana::Foldable
<S
>::value
,
51 "hana::fold_right(xs, f) requires 'xs' to be Foldable");
54 return FoldRight::apply(static_cast<Xs
&&>(xs
), static_cast<F
&&>(f
));
59 template <typename F
, typename State
>
60 struct variadic_foldr
{
63 template <typename
...T
>
64 constexpr decltype(auto) operator()(T
&& ...t
) const {
65 return detail::variadic::foldr(
67 static_cast<State
&&>(state
),
68 static_cast<T
&&>(t
)...
74 template <typename T
, bool condition
>
75 struct fold_right_impl
<T
, when
<condition
>> : default_
{
77 template <typename Xs
, typename S
, typename F
>
78 static constexpr decltype(auto) apply(Xs
&& xs
, S
&& s
, F
&& f
) {
79 return hana::unpack(static_cast<Xs
&&>(xs
),
80 detail::variadic_foldr
<F
, S
>{f
, s
}
85 template <typename Xs
, typename F
>
86 static constexpr decltype(auto) apply(Xs
&& xs
, F
&& f
) {
87 return hana::unpack(static_cast<Xs
&&>(xs
),
89 detail::variadic::foldr1
,
95 BOOST_HANA_NAMESPACE_END
97 #endif // !BOOST_HANA_FOLD_RIGHT_HPP