fold.hpp 4.9 KB

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