3 Forward declares `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_FWD_FOLD_RIGHT_HPP
11 #define BOOST_HANA_FWD_FOLD_RIGHT_HPP
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/core/when.hpp>
17 BOOST_HANA_NAMESPACE_BEGIN
18 //! Right-fold of a structure using a binary operation and an optional
19 //! initial reduction state.
20 //! @ingroup group-Foldable
22 //! `fold_right` is a right-associative fold using a binary operation.
23 //! Given a structure containing `x1, ..., xn`, a function `f` and
24 //! an optional initial state, `fold_right` applies `f` as follows
26 //! f(x1, f(x2, f(x3, f(x4, ... f(xn-1, xn) ... )))) // without state
27 //! f(x1, f(x2, f(x3, f(x4, ... f(xn, state) ... )))) // with state
31 //! It is worth noting that the order in which the binary function should
32 //! expect its arguments is reversed from `fold_left`.
34 //! When the structure is empty, two things may arise. If an initial
35 //! state was provided, it is returned as-is. Otherwise, if the no-state
36 //! version of the function was used, an error is triggered. When the
37 //! stucture contains a single element and the no-state version of the
38 //! function was used, that single element is returned as is.
43 //! Given a `Foldable` `F` and an optional initial state of tag `S`,
44 //! the signatures for `fold_right` are
46 //! \mathtt{fold\_right} : F(T) \times S \times (T \times S \to S) \to S
49 //! for the variant with an initial state, and
51 //! \mathtt{fold\_right} : F(T) \times (T \times T \to T) \to T
54 //! for the variant without an initial state.
57 //! The structure to fold.
60 //! The initial value used for folding.
63 //! A binary function called as `f(x, state)`, where `state` is the
64 //! result accumulated so far and `x` is an element in the structure.
65 //! For right folds without an initial state, the function is called as
66 //! `f(x1, x2)`, where `x1` and `x2` are elements of the structure.
71 //! @include example/fold_right.cpp
72 #ifdef BOOST_HANA_DOXYGEN_INVOKED
73 constexpr auto fold_right = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
74 return tag-dispatched;
77 template <typename T, typename = void>
78 struct fold_right_impl : fold_right_impl<T, when<true>> { };
81 template <typename Xs, typename State, typename F>
82 constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
84 template <typename Xs, typename F>
85 constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
88 constexpr fold_right_t fold_right{};
90 BOOST_HANA_NAMESPACE_END
92 #endif // !BOOST_HANA_FWD_FOLD_RIGHT_HPP