reverse_fold.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*!
  2. @file
  3. Forward declares `boost::hana::reverse_fold`.
  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_REVERSE_FOLD_HPP
  9. #define BOOST_HANA_FWD_REVERSE_FOLD_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! Equivalent to `reverse_fold` in Boost.Fusion and Boost.MPL.
  13. //! @ingroup group-Foldable
  14. //!
  15. //! This method has the same semantics as `reverse_fold` in Boost.Fusion
  16. //! and Boost.MPL, with the extension that an initial state is not
  17. //! required. This method is equivalent to `fold_right`, except that
  18. //! the accumulating function must take its arguments in reverse order,
  19. //! to match the order used in Fusion. In other words,
  20. //! @code
  21. //! reverse_fold(sequence, state, f) == fold_right(sequence, state, flip(f))
  22. //! reverse_fold(sequence, f) == fold_right(sequence, flip(f))
  23. //! @endcode
  24. //!
  25. //! @note
  26. //! This method is a convenience alias to `fold_right`. As an alias,
  27. //! `reverse_fold` is not tag-dispatched on its own and `fold_right`
  28. //! should be customized instead.
  29. //!
  30. //!
  31. //! Signature
  32. //! ---------
  33. //! Given a `Foldable` `F` and an optional initial state of tag `S`,
  34. //! the signatures for `reverse_fold` are
  35. //! \f[
  36. //! \mathtt{reverse\_fold} : F(T) \times S \times (S \times T \to S) \to S
  37. //! \f]
  38. //!
  39. //! for the variant with an initial state, and
  40. //! \f[
  41. //! \mathtt{reverse\_fold} : F(T) \times (T \times T \to T) \to T
  42. //! \f]
  43. //!
  44. //! for the variant without an initial state.
  45. //!
  46. //! @param xs
  47. //! The structure to fold.
  48. //!
  49. //! @param state
  50. //! The initial value used for folding.
  51. //!
  52. //! @param f
  53. //! A binary function called as `f(state, x)`, where `state` is the
  54. //! result accumulated so far and `x` is an element in the structure.
  55. //! For reverse folds without an initial state, the function is called as
  56. //! `f(x1, x2)`, where `x1` and `x2` are elements of the structure.
  57. //!
  58. //!
  59. //! Example
  60. //! -------
  61. //! @include example/reverse_fold.cpp
  62. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  63. constexpr auto reverse_fold = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
  64. return fold_right(forwarded(xs), forwarded(state), flip(forwarded(f)));
  65. };
  66. #else
  67. struct reverse_fold_t {
  68. template <typename Xs, typename S, typename F>
  69. constexpr decltype(auto) operator()(Xs&& xs, S&& s, F&& f) const;
  70. template <typename Xs, typename F>
  71. constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
  72. };
  73. constexpr reverse_fold_t reverse_fold{};
  74. #endif
  75. BOOST_HANA_NAMESPACE_END
  76. #endif // !BOOST_HANA_FWD_REVERSE_FOLD_HPP