Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:53:04

0001 /*!
0002 @file
0003 Forward declares `boost::hana::monadic_fold_right`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP
0011 #define BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/core/when.hpp>
0015 
0016 
0017 namespace boost { namespace hana {
0018     //! Monadic right-fold of a structure with a binary operation and an
0019     //! optional initial reduction state.
0020     //! @ingroup group-Foldable
0021     //!
0022     //! @note
0023     //! This assumes the reader to be accustomed to non-monadic right-folds as
0024     //! explained by `hana::fold_right`, and to have read the [primer]
0025     //! (@ref monadic-folds) on monadic folds.
0026     //!
0027     //! `monadic_fold_right<M>` is a right-associative monadic fold. Given a
0028     //! structure containing `x1, ..., xn`, a function `f` and an optional
0029     //! initial state, `monadic_fold_right<M>` applies `f` as follows
0030     //! @code
0031     //!     // with state
0032     //!     (f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn, state)))))
0033     //!
0034     //!     // without state
0035     //!     (f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn-1, xn)))))
0036     //! @endcode
0037     //!
0038     //! where `f(xk, -)` denotes the partial application of `f` to `xk`,
0039     //! and `|` is just the operator version of the monadic `chain`.
0040     //! It is worth noting that the order in which the binary function should
0041     //! expect its arguments is reversed from `monadic_fold_left<M>`.
0042     //!
0043     //! When the structure is empty, one of two things may happen. If an
0044     //! initial state was provided, it is lifted to the given Monad and
0045     //! returned as-is. Otherwise, if the no-state version of the function
0046     //! was used, an error is triggered. When the stucture contains a single
0047     //! element and the no-state version of the function was used, that
0048     //! single element is lifted into the given Monad and returned as is.
0049     //!
0050     //!
0051     //! Signature
0052     //! ---------
0053     //! Given a `Monad` `M`, a `Foldable` `F`, an initial state of tag `S`,
0054     //! and a function @f$ f : T \times S \to M(S) @f$, the signatures of
0055     //! `monadic_fold_right<M>` are
0056     //! \f[
0057     //!     \mathtt{monadic\_fold\_right}_M :
0058     //!         F(T) \times S \times (T \times S \to M(S)) \to M(S)
0059     //! \f]
0060     //!
0061     //! for the version with an initial state, and
0062     //! \f[
0063     //!     \mathtt{monadic\_fold\_right}_M :
0064     //!         F(T) \times (T \times T \to M(T)) \to M(T)
0065     //! \f]
0066     //!
0067     //! for the version without an initial state.
0068     //!
0069     //! @tparam M
0070     //! The Monad representing the monadic context in which the fold happens.
0071     //! The return type of `f` must be in that Monad.
0072     //!
0073     //! @param xs
0074     //! The structure to fold.
0075     //!
0076     //! @param state
0077     //! The initial value used for folding. If the structure is empty, this
0078     //! value is lifted in to the `M` Monad and then returned as-is.
0079     //!
0080     //! @param f
0081     //! A binary function called as `f(x, state)`, where `state` is the result
0082     //! accumulated so far and `x` is an element in the structure. The
0083     //! function must return its result inside the `M` Monad.
0084     //!
0085     //!
0086     //! Example
0087     //! -------
0088     //! @include example/monadic_fold_right.cpp
0089 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0090     template <typename M>
0091     constexpr auto monadic_fold_right = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
0092         return tag-dispatched;
0093     };
0094 #else
0095     template <typename T, typename = void>
0096     struct monadic_fold_right_impl : monadic_fold_right_impl<T, when<true>> { };
0097 
0098     template <typename M>
0099     struct monadic_fold_right_t {
0100         template <typename Xs, typename State, typename F>
0101         constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
0102 
0103         template <typename Xs, typename F>
0104         constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
0105     };
0106 
0107     template <typename M>
0108     BOOST_HANA_INLINE_VARIABLE constexpr monadic_fold_right_t<M> monadic_fold_right{};
0109 #endif
0110 }} // end namespace boost::hana
0111 
0112 #endif // !BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP