flow.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. flow.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_FUNCTION_FLOW_H
  8. #define BOOST_HOF_GUARD_FUNCTION_FLOW_H
  9. /// flow
  10. /// ====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `flow` function adaptor provides function composition. It is useful as
  16. /// an alternative to using the pipe operator `|` when chaining functions. It is
  17. /// similiar to [`compose`](compose.md) except the evaluation order is
  18. /// reversed. So, `flow(f, g)(0)` is equivalent to `g(f(0))`.
  19. ///
  20. ///
  21. /// Synopsis
  22. /// --------
  23. ///
  24. /// template<class... Fs>
  25. /// constexpr flow_adaptor<Fs...> flow(Fs... fs);
  26. ///
  27. /// Semantics
  28. /// ---------
  29. ///
  30. /// assert(flow(f, g)(xs...) == g(f(xs...)));
  31. ///
  32. /// Requirements
  33. /// ------------
  34. ///
  35. /// Fs must be:
  36. ///
  37. /// * [ConstInvocable](ConstInvocable)
  38. /// * MoveConstructible
  39. ///
  40. /// Example
  41. /// -------
  42. ///
  43. /// #include <boost/hof.hpp>
  44. /// #include <cassert>
  45. /// using namespace boost::hof;
  46. ///
  47. /// struct increment
  48. /// {
  49. /// template<class T>
  50. /// T operator()(T x) const
  51. /// {
  52. /// return x + 1;
  53. /// }
  54. /// };
  55. ///
  56. /// struct decrement
  57. /// {
  58. /// template<class T>
  59. /// T operator()(T x) const
  60. /// {
  61. /// return x - 1;
  62. /// }
  63. /// };
  64. ///
  65. /// int main() {
  66. /// int r = flow(increment(), decrement(), increment())(3);
  67. /// assert(r == 4);
  68. /// }
  69. ///
  70. /// References
  71. /// ----------
  72. ///
  73. /// * [Function composition](https://en.wikipedia.org/wiki/Function_composition)
  74. ///
  75. #include <boost/hof/detail/callable_base.hpp>
  76. #include <boost/hof/always.hpp>
  77. #include <boost/hof/detail/delegate.hpp>
  78. #include <boost/hof/detail/compressed_pair.hpp>
  79. #include <boost/hof/detail/join.hpp>
  80. #include <tuple>
  81. #include <boost/hof/detail/move.hpp>
  82. #include <boost/hof/detail/make.hpp>
  83. #include <boost/hof/detail/result_type.hpp>
  84. #include <boost/hof/detail/static_const_var.hpp>
  85. namespace boost { namespace hof { namespace detail {
  86. template<class F1, class F2>
  87. struct flow_kernel : detail::compressed_pair<detail::callable_base<F1>, detail::callable_base<F2>>, compose_function_result_type<F2, F1>
  88. {
  89. typedef detail::compressed_pair<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
  90. BOOST_HOF_INHERIT_CONSTRUCTOR(flow_kernel, base_type)
  91. BOOST_HOF_RETURNS_CLASS(flow_kernel);
  92. template<class... Ts>
  93. constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F2>&, result_of<const detail::callable_base<F1>&, id_<Ts>...>)
  94. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  95. (
  96. BOOST_HOF_MANGLE_CAST(const detail::callable_base<F2>&)(BOOST_HOF_CONST_THIS->second(xs...))(
  97. BOOST_HOF_MANGLE_CAST(const detail::callable_base<F1>&)(BOOST_HOF_CONST_THIS->first(xs...))(BOOST_HOF_FORWARD(Ts)(xs)...)
  98. )
  99. );
  100. };
  101. }
  102. template<class F, class... Fs>
  103. struct flow_adaptor : detail::flow_kernel<F, BOOST_HOF_JOIN(flow_adaptor, Fs...)>
  104. {
  105. typedef flow_adaptor fit_rewritable_tag;
  106. typedef BOOST_HOF_JOIN(flow_adaptor, Fs...) tail;
  107. typedef detail::flow_kernel<F, tail> base_type;
  108. BOOST_HOF_INHERIT_DEFAULT(flow_adaptor, base_type)
  109. template<class X, class... Xs,
  110. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X),
  111. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(tail, Xs...)
  112. >
  113. constexpr flow_adaptor(X&& f1, Xs&& ... fs)
  114. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base_type, X&&, tail) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(tail, Xs&&...))
  115. : base_type(BOOST_HOF_FORWARD(X)(f1), tail(BOOST_HOF_FORWARD(Xs)(fs)...))
  116. {}
  117. template<class X,
  118. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X)
  119. >
  120. constexpr flow_adaptor(X&& f1)
  121. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base_type, X&&)
  122. : base_type(BOOST_HOF_FORWARD(X)(f1))
  123. {}
  124. };
  125. template<class F>
  126. struct flow_adaptor<F> : detail::callable_base<F>
  127. {
  128. typedef flow_adaptor fit_rewritable_tag;
  129. BOOST_HOF_INHERIT_DEFAULT(flow_adaptor, detail::callable_base<F>)
  130. template<class X, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>)>
  131. constexpr flow_adaptor(X&& f1)
  132. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(detail::callable_base<F>, X&&)
  133. : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1))
  134. {}
  135. };
  136. template<class F1, class F2>
  137. struct flow_adaptor<F1, F2>
  138. : detail::flow_kernel<detail::callable_base<F1>, detail::callable_base<F2>>
  139. {
  140. typedef flow_adaptor fit_rewritable_tag;
  141. typedef detail::flow_kernel<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
  142. BOOST_HOF_INHERIT_CONSTRUCTOR(flow_adaptor, base_type)
  143. };
  144. BOOST_HOF_DECLARE_STATIC_VAR(flow, detail::make<flow_adaptor>);
  145. }} // namespace boost::hof
  146. #endif