monadic_fold_right.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*!
  2. @file
  3. Forward declares `boost::hana::monadic_fold_right`.
  4. Copyright Louis Dionne 2013-2022
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP
  9. #define BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. namespace boost { namespace hana {
  13. //! Monadic right-fold of a structure with a binary operation and an
  14. //! optional initial reduction state.
  15. //! @ingroup group-Foldable
  16. //!
  17. //! @note
  18. //! This assumes the reader to be accustomed to non-monadic right-folds as
  19. //! explained by `hana::fold_right`, and to have read the [primer]
  20. //! (@ref monadic-folds) on monadic folds.
  21. //!
  22. //! `monadic_fold_right<M>` is a right-associative monadic fold. Given a
  23. //! structure containing `x1, ..., xn`, a function `f` and an optional
  24. //! initial state, `monadic_fold_right<M>` applies `f` as follows
  25. //! @code
  26. //! // with state
  27. //! (f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn, state)))))
  28. //!
  29. //! // without state
  30. //! (f(x1, -) | (f(x2, -) | (f(x3, -) | (... | f(xn-1, xn)))))
  31. //! @endcode
  32. //!
  33. //! where `f(xk, -)` denotes the partial application of `f` to `xk`,
  34. //! and `|` is just the operator version of the monadic `chain`.
  35. //! It is worth noting that the order in which the binary function should
  36. //! expect its arguments is reversed from `monadic_fold_left<M>`.
  37. //!
  38. //! When the structure is empty, one of two things may happen. If an
  39. //! initial state was provided, it is lifted to the given Monad and
  40. //! returned as-is. Otherwise, if the no-state version of the function
  41. //! was used, an error is triggered. When the stucture contains a single
  42. //! element and the no-state version of the function was used, that
  43. //! single element is lifted into the given Monad and returned as is.
  44. //!
  45. //!
  46. //! Signature
  47. //! ---------
  48. //! Given a `Monad` `M`, a `Foldable` `F`, an initial state of tag `S`,
  49. //! and a function @f$ f : T \times S \to M(S) @f$, the signatures of
  50. //! `monadic_fold_right<M>` are
  51. //! \f[
  52. //! \mathtt{monadic\_fold\_right}_M :
  53. //! F(T) \times S \times (T \times S \to M(S)) \to M(S)
  54. //! \f]
  55. //!
  56. //! for the version with an initial state, and
  57. //! \f[
  58. //! \mathtt{monadic\_fold\_right}_M :
  59. //! F(T) \times (T \times T \to M(T)) \to M(T)
  60. //! \f]
  61. //!
  62. //! for the version without an initial state.
  63. //!
  64. //! @tparam M
  65. //! The Monad representing the monadic context in which the fold happens.
  66. //! The return type of `f` must be in that Monad.
  67. //!
  68. //! @param xs
  69. //! The structure to fold.
  70. //!
  71. //! @param state
  72. //! The initial value used for folding. If the structure is empty, this
  73. //! value is lifted in to the `M` Monad and then returned as-is.
  74. //!
  75. //! @param f
  76. //! A binary function called as `f(x, state)`, where `state` is the result
  77. //! accumulated so far and `x` is an element in the structure. The
  78. //! function must return its result inside the `M` Monad.
  79. //!
  80. //!
  81. //! Example
  82. //! -------
  83. //! @include example/monadic_fold_right.cpp
  84. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  85. template <typename M>
  86. constexpr auto monadic_fold_right = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
  87. return tag-dispatched;
  88. };
  89. #else
  90. template <typename T, typename = void>
  91. struct monadic_fold_right_impl : monadic_fold_right_impl<T, when<true>> { };
  92. template <typename M>
  93. struct monadic_fold_right_t {
  94. template <typename Xs, typename State, typename F>
  95. constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
  96. template <typename Xs, typename F>
  97. constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
  98. };
  99. template <typename M>
  100. BOOST_HANA_INLINE_VARIABLE constexpr monadic_fold_right_t<M> monadic_fold_right{};
  101. #endif
  102. }} // end namespace boost::hana
  103. #endif // !BOOST_HANA_FWD_MONADIC_FOLD_RIGHT_HPP