monadic_fold_left.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. @file
  3. Forward declares `boost::hana::monadic_fold_left`.
  4. @copyright Louis Dionne 2013-2017
  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_LEFT_HPP
  9. #define BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Monadic left-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 left-folds as
  19. //! explained by `hana::fold_left`, and to have read the [primer]
  20. //! (@ref monadic-folds) on monadic folds.
  21. //!
  22. //! `monadic_fold_left<M>` is a left-associative monadic fold. Given a
  23. //! `Foldable` with linearization `[x1, ..., xn]`, a function `f` and an
  24. //! optional initial state, `monadic_fold_left<M>` applies `f` as follows:
  25. //! @code
  26. //! // with state
  27. //! ((((f(state, x1) | f(-, x2)) | f(-, x3)) | ...) | f(-, xn))
  28. //!
  29. //! // without state
  30. //! ((((f(x1, x2) | f(-, x3)) | f(-, x4)) | ...) | f(-, xn))
  31. //! @endcode
  32. //!
  33. //! where `f(-, xk)` denotes the partial application of `f` to `xk`, and
  34. //! `|` is just the operator version of the monadic `chain`.
  35. //!
  36. //! When the structure is empty, one of two things may happen. If an
  37. //! initial state was provided, it is lifted to the given Monad and
  38. //! returned as-is. Otherwise, if the no-state version of the function
  39. //! was used, an error is triggered. When the stucture contains a single
  40. //! element and the no-state version of the function was used, that
  41. //! single element is lifted into the given Monad and returned as is.
  42. //!
  43. //!
  44. //! Signature
  45. //! ---------
  46. //! Given a `Monad` `M`, a `Foldable` `F`, an initial state of tag `S`,
  47. //! and a function @f$ f : S \times T \to M(S) @f$, the signatures of
  48. //! `monadic_fold_left<M>` are
  49. //! \f[
  50. //! \mathtt{monadic\_fold\_left}_M :
  51. //! F(T) \times S \times (S \times T \to M(S)) \to M(S)
  52. //! \f]
  53. //!
  54. //! for the version with an initial state, and
  55. //! \f[
  56. //! \mathtt{monadic\_fold\_left}_M :
  57. //! F(T) \times (T \times T \to M(T)) \to M(T)
  58. //! \f]
  59. //!
  60. //! for the version without an initial state.
  61. //!
  62. //! @tparam M
  63. //! The Monad representing the monadic context in which the fold happens.
  64. //! The return type of `f` must be in that Monad.
  65. //!
  66. //! @param xs
  67. //! The structure to fold.
  68. //!
  69. //! @param state
  70. //! The initial value used for folding. If the structure is empty, this
  71. //! value is lifted in to the `M` Monad and then returned as-is.
  72. //!
  73. //! @param f
  74. //! A binary function called as `f(state, x)`, where `state` is the result
  75. //! accumulated so far and `x` is an element in the structure. The
  76. //! function must return its result inside the `M` Monad.
  77. //!
  78. //!
  79. //! Example
  80. //! -------
  81. //! @include example/monadic_fold_left.cpp
  82. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  83. template <typename M>
  84. constexpr auto monadic_fold_left = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
  85. return tag-dispatched;
  86. };
  87. #else
  88. template <typename T, typename = void>
  89. struct monadic_fold_left_impl : monadic_fold_left_impl<T, when<true>> { };
  90. template <typename M>
  91. struct monadic_fold_left_t {
  92. template <typename Xs, typename State, typename F>
  93. constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
  94. template <typename Xs, typename F>
  95. constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
  96. };
  97. template <typename M>
  98. constexpr monadic_fold_left_t<M> monadic_fold_left{};
  99. #endif
  100. BOOST_HANA_NAMESPACE_END
  101. #endif // !BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP