fold_right.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*!
  2. @file
  3. Forward declares `boost::hana::fold_right`.
  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_FOLD_RIGHT_HPP
  9. #define BOOST_HANA_FWD_FOLD_RIGHT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Right-fold of a structure using a binary operation and an optional
  14. //! initial reduction state.
  15. //! @ingroup group-Foldable
  16. //!
  17. //! `fold_right` is a right-associative fold using a binary operation.
  18. //! Given a structure containing `x1, ..., xn`, a function `f` and
  19. //! an optional initial state, `fold_right` applies `f` as follows
  20. //! @code
  21. //! f(x1, f(x2, f(x3, f(x4, ... f(xn-1, xn) ... )))) // without state
  22. //! f(x1, f(x2, f(x3, f(x4, ... f(xn, state) ... )))) // with state
  23. //! @endcode
  24. //!
  25. //! @note
  26. //! It is worth noting that the order in which the binary function should
  27. //! expect its arguments is reversed from `fold_left`.
  28. //!
  29. //! When the structure is empty, two things may arise. If an initial
  30. //! state was provided, it is returned as-is. Otherwise, if the no-state
  31. //! version of the function was used, an error is triggered. When the
  32. //! stucture contains a single element and the no-state version of the
  33. //! function was used, that single element is returned as is.
  34. //!
  35. //!
  36. //! Signature
  37. //! ---------
  38. //! Given a `Foldable` `F` and an optional initial state of tag `S`,
  39. //! the signatures for `fold_right` are
  40. //! \f[
  41. //! \mathtt{fold\_right} : F(T) \times S \times (T \times S \to S) \to S
  42. //! \f]
  43. //!
  44. //! for the variant with an initial state, and
  45. //! \f[
  46. //! \mathtt{fold\_right} : F(T) \times (T \times T \to T) \to T
  47. //! \f]
  48. //!
  49. //! for the variant without an initial state.
  50. //!
  51. //! @param xs
  52. //! The structure to fold.
  53. //!
  54. //! @param state
  55. //! The initial value used for folding.
  56. //!
  57. //! @param f
  58. //! A binary function called as `f(x, state)`, where `state` is the
  59. //! result accumulated so far and `x` is an element in the structure.
  60. //! For right folds without an initial state, the function is called as
  61. //! `f(x1, x2)`, where `x1` and `x2` are elements of the structure.
  62. //!
  63. //!
  64. //! Example
  65. //! -------
  66. //! @include example/fold_right.cpp
  67. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  68. constexpr auto fold_right = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
  69. return tag-dispatched;
  70. };
  71. #else
  72. template <typename T, typename = void>
  73. struct fold_right_impl : fold_right_impl<T, when<true>> { };
  74. struct fold_right_t {
  75. template <typename Xs, typename State, typename F>
  76. constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
  77. template <typename Xs, typename F>
  78. constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
  79. };
  80. constexpr fold_right_t fold_right{};
  81. #endif
  82. BOOST_HANA_NAMESPACE_END
  83. #endif // !BOOST_HANA_FWD_FOLD_RIGHT_HPP