reverse_fold.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. reverse_fold.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_REVERSE_FOLD_H
  8. #define BOOST_HOF_GUARD_REVERSE_FOLD_H
  9. /// reverse_fold
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `reverse_fold` function adaptor uses a binary function to apply a
  16. /// reverse [fold]
  17. /// (https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29)(ie right
  18. /// fold in functional programming terms) operation to the arguments passed to
  19. /// the function. Additionally, an optional initial state can be provided,
  20. /// otherwise the first argument is used as the initial state.
  21. ///
  22. /// The arguments to the binary function, take first the state and then the
  23. /// argument.
  24. ///
  25. /// Synopsis
  26. /// --------
  27. ///
  28. /// template<class F, class State>
  29. /// constexpr reverse_fold_adaptor<F, State> reverse_fold(F f, State s);
  30. ///
  31. /// template<class F>
  32. /// constexpr reverse_fold_adaptor<F> reverse_fold(F f);
  33. ///
  34. /// Semantics
  35. /// ---------
  36. ///
  37. /// assert(reverse_fold(f, z)() == z);
  38. /// assert(reverse_fold(f, z)(x, xs...) == f(reverse_fold(f, z)(xs...), x));
  39. /// assert(reverse_fold(f)(x) == x);
  40. /// assert(reverse_fold(f)(x, xs...) == f(reverse_fold(f)(xs...), x));
  41. ///
  42. /// Requirements
  43. /// ------------
  44. ///
  45. /// State must be:
  46. ///
  47. /// * CopyConstructible
  48. ///
  49. /// F must be:
  50. ///
  51. /// * [BinaryInvocable](BinaryInvocable)
  52. /// * MoveConstructible
  53. ///
  54. /// Example
  55. /// -------
  56. ///
  57. /// #include <boost/hof.hpp>
  58. /// #include <cassert>
  59. ///
  60. /// struct max_f
  61. /// {
  62. /// template<class T, class U>
  63. /// constexpr T operator()(T x, U y) const
  64. /// {
  65. /// return x > y ? x : y;
  66. /// }
  67. /// };
  68. ///
  69. /// int main() {
  70. /// assert(boost::hof::reverse_fold(max_f())(2, 3, 4, 5) == 5);
  71. /// }
  72. ///
  73. /// References
  74. /// ----------
  75. ///
  76. /// * [Projections](Projections)
  77. /// * [Variadic print](<Variadic print>)
  78. ///
  79. #include <boost/hof/detail/callable_base.hpp>
  80. #include <boost/hof/detail/delegate.hpp>
  81. #include <boost/hof/detail/compressed_pair.hpp>
  82. #include <boost/hof/detail/move.hpp>
  83. #include <boost/hof/detail/make.hpp>
  84. #include <boost/hof/detail/static_const_var.hpp>
  85. namespace boost { namespace hof { namespace detail {
  86. struct v_reverse_fold
  87. {
  88. BOOST_HOF_RETURNS_CLASS(v_reverse_fold);
  89. template<class F, class State, class T, class... Ts>
  90. constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(const F&, result_of<const v_reverse_fold&, id_<const F&>, id_<State>, id_<Ts>...>, id_<T>)
  91. operator()(const F& f, State&& state, T&& x, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS
  92. (
  93. f((*BOOST_HOF_CONST_THIS)(f, BOOST_HOF_FORWARD(State)(state), BOOST_HOF_FORWARD(Ts)(xs)...), BOOST_HOF_FORWARD(T)(x))
  94. );
  95. template<class F, class State>
  96. constexpr State operator()(const F&, State&& state) const noexcept
  97. {
  98. return BOOST_HOF_FORWARD(State)(state);
  99. }
  100. };
  101. }
  102. template<class F, class State=void>
  103. struct reverse_fold_adaptor
  104. : detail::compressed_pair<detail::callable_base<F>, State>
  105. {
  106. typedef detail::compressed_pair<detail::callable_base<F>, State> base_type;
  107. BOOST_HOF_INHERIT_CONSTRUCTOR(reverse_fold_adaptor, base_type)
  108. template<class... Ts>
  109. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
  110. {
  111. return this->first(xs...);
  112. }
  113. template<class... Ts>
  114. constexpr State get_state(Ts&&... xs) const noexcept
  115. {
  116. return this->second(xs...);
  117. }
  118. BOOST_HOF_RETURNS_CLASS(reverse_fold_adaptor);
  119. template<class... Ts>
  120. constexpr BOOST_HOF_SFINAE_RESULT(detail::v_reverse_fold, id_<const detail::callable_base<F>&>, id_<State>, id_<Ts>...)
  121. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  122. (
  123. detail::v_reverse_fold()(
  124. BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
  125. BOOST_HOF_MANGLE_CAST(State)(BOOST_HOF_CONST_THIS->get_state(xs...)),
  126. BOOST_HOF_FORWARD(Ts)(xs)...
  127. )
  128. )
  129. };
  130. template<class F>
  131. struct reverse_fold_adaptor<F, void>
  132. : detail::callable_base<F>
  133. {
  134. BOOST_HOF_INHERIT_CONSTRUCTOR(reverse_fold_adaptor, detail::callable_base<F>)
  135. template<class... Ts>
  136. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
  137. {
  138. return boost::hof::always_ref(*this)(xs...);
  139. }
  140. BOOST_HOF_RETURNS_CLASS(reverse_fold_adaptor);
  141. template<class... Ts>
  142. constexpr BOOST_HOF_SFINAE_RESULT(detail::v_reverse_fold, id_<const detail::callable_base<F>&>, id_<Ts>...)
  143. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  144. (
  145. detail::v_reverse_fold()(
  146. BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
  147. BOOST_HOF_FORWARD(Ts)(xs)...
  148. )
  149. )
  150. };
  151. BOOST_HOF_DECLARE_STATIC_VAR(reverse_fold, detail::make<reverse_fold_adaptor>);
  152. }} // namespace boost::hof
  153. #endif