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_left`.
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_LEFT_HPP
0011 #define BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/core/when.hpp>
0015 
0016 
0017 namespace boost { namespace hana {
0018     //! Monadic left-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 left-folds as
0024     //! explained by `hana::fold_left`, and to have read the [primer]
0025     //! (@ref monadic-folds) on monadic folds.
0026     //!
0027     //! `monadic_fold_left<M>` is a left-associative monadic fold. Given a
0028     //! `Foldable` with linearization `[x1, ..., xn]`, a function `f` and an
0029     //! optional initial state, `monadic_fold_left<M>` applies `f` as follows:
0030     //! @code
0031     //!     // with state
0032     //!     ((((f(state, x1) | f(-, x2)) | f(-, x3)) | ...) | f(-, xn))
0033     //!
0034     //!     // without state
0035     //!     ((((f(x1, x2) | f(-, x3)) | f(-, x4)) | ...) | f(-, xn))
0036     //! @endcode
0037     //!
0038     //! where `f(-, xk)` denotes the partial application of `f` to `xk`, and
0039     //! `|` is just the operator version of the monadic `chain`.
0040     //!
0041     //! When the structure is empty, one of two things may happen. If an
0042     //! initial state was provided, it is lifted to the given Monad and
0043     //! returned as-is. Otherwise, if the no-state version of the function
0044     //! was used, an error is triggered. When the stucture contains a single
0045     //! element and the no-state version of the function was used, that
0046     //! single element is lifted into the given Monad and returned as is.
0047     //!
0048     //!
0049     //! Signature
0050     //! ---------
0051     //! Given a `Monad` `M`, a `Foldable` `F`, an initial state of tag `S`,
0052     //! and a function @f$ f : S \times T \to M(S) @f$, the signatures of
0053     //! `monadic_fold_left<M>` are
0054     //! \f[
0055     //!     \mathtt{monadic\_fold\_left}_M :
0056     //!         F(T) \times S \times (S \times T \to M(S)) \to M(S)
0057     //! \f]
0058     //!
0059     //! for the version with an initial state, and
0060     //! \f[
0061     //!     \mathtt{monadic\_fold\_left}_M :
0062     //!         F(T) \times (T \times T \to M(T)) \to M(T)
0063     //! \f]
0064     //!
0065     //! for the version without an initial state.
0066     //!
0067     //! @tparam M
0068     //! The Monad representing the monadic context in which the fold happens.
0069     //! The return type of `f` must be in that Monad.
0070     //!
0071     //! @param xs
0072     //! The structure to fold.
0073     //!
0074     //! @param state
0075     //! The initial value used for folding. If the structure is empty, this
0076     //! value is lifted in to the `M` Monad and then returned as-is.
0077     //!
0078     //! @param f
0079     //! A binary function called as `f(state, x)`, where `state` is the result
0080     //! accumulated so far and `x` is an element in the structure. The
0081     //! function must return its result inside the `M` Monad.
0082     //!
0083     //!
0084     //! Example
0085     //! -------
0086     //! @include example/monadic_fold_left.cpp
0087 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0088     template <typename M>
0089     constexpr auto monadic_fold_left = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
0090         return tag-dispatched;
0091     };
0092 #else
0093     template <typename T, typename = void>
0094     struct monadic_fold_left_impl : monadic_fold_left_impl<T, when<true>> { };
0095 
0096     template <typename M>
0097     struct monadic_fold_left_t {
0098         template <typename Xs, typename State, typename F>
0099         constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
0100 
0101         template <typename Xs, typename F>
0102         constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
0103     };
0104 
0105     template <typename M>
0106     BOOST_HANA_INLINE_VARIABLE constexpr monadic_fold_left_t<M> monadic_fold_left{};
0107 #endif
0108 }} // end namespace boost::hana
0109 
0110 #endif // !BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP