compose.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. compose.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_COMPOSE_H
  8. #define BOOST_HOF_GUARD_FUNCTION_COMPOSE_H
  9. /// compose
  10. /// =======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `compose` function adaptor provides function composition. It produces
  16. /// a function object that composes a set of functions, ie the output of one
  17. /// function becomes the input of the second function. So, `compose(f, g)(0)`
  18. /// is equivalent to `f(g(0))`.
  19. ///
  20. ///
  21. /// Synopsis
  22. /// --------
  23. ///
  24. /// template<class... Fs>
  25. /// constexpr compose_adaptor<Fs...> compose(Fs... fs);
  26. ///
  27. /// Semantics
  28. /// ---------
  29. ///
  30. /// assert(compose(f, g)(xs...) == f(g(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 = compose(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. ///
  76. #include <boost/hof/detail/callable_base.hpp>
  77. #include <boost/hof/always.hpp>
  78. #include <boost/hof/detail/delegate.hpp>
  79. #include <boost/hof/detail/compressed_pair.hpp>
  80. #include <boost/hof/detail/join.hpp>
  81. #include <tuple>
  82. #include <boost/hof/detail/move.hpp>
  83. #include <boost/hof/detail/make.hpp>
  84. #include <boost/hof/detail/result_type.hpp>
  85. #include <boost/hof/detail/static_const_var.hpp>
  86. namespace boost { namespace hof { namespace detail {
  87. template<class F1, class F2>
  88. struct compose_kernel : detail::compressed_pair<F1, F2>, compose_function_result_type<F1, F2>
  89. {
  90. typedef detail::compressed_pair<F1, F2> base_type;
  91. BOOST_HOF_INHERIT_CONSTRUCTOR(compose_kernel, base_type)
  92. BOOST_HOF_RETURNS_CLASS(compose_kernel);
  93. template<class... Ts>
  94. constexpr BOOST_HOF_SFINAE_RESULT(const F1&, result_of<const F2&, id_<Ts>...>)
  95. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  96. (
  97. BOOST_HOF_MANGLE_CAST(const F1&)(BOOST_HOF_CONST_THIS->first(xs...))(
  98. BOOST_HOF_MANGLE_CAST(const F2&)(BOOST_HOF_CONST_THIS->second(xs...))(BOOST_HOF_FORWARD(Ts)(xs)...)
  99. )
  100. );
  101. };
  102. }
  103. template<class F, class... Fs>
  104. struct compose_adaptor
  105. : detail::compose_kernel<detail::callable_base<F>, BOOST_HOF_JOIN(compose_adaptor, detail::callable_base<Fs>...)>
  106. {
  107. typedef compose_adaptor fit_rewritable_tag;
  108. typedef BOOST_HOF_JOIN(compose_adaptor, detail::callable_base<Fs>...) tail;
  109. typedef detail::compose_kernel<detail::callable_base<F>, tail> base_type;
  110. BOOST_HOF_INHERIT_DEFAULT(compose_adaptor, base_type)
  111. template<class X, class... Xs,
  112. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X),
  113. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(tail, Xs...)
  114. >
  115. constexpr compose_adaptor(X&& f1, Xs&& ... fs)
  116. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base_type, X&&, tail) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(tail, Xs&&...))
  117. : base_type(BOOST_HOF_FORWARD(X)(f1), tail(BOOST_HOF_FORWARD(Xs)(fs)...))
  118. {}
  119. template<class X,
  120. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X)
  121. >
  122. constexpr compose_adaptor(X&& f1)
  123. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base_type, X&&)
  124. : base_type(BOOST_HOF_FORWARD(X)(f1))
  125. {}
  126. };
  127. template<class F>
  128. struct compose_adaptor<F> : detail::callable_base<F>
  129. {
  130. typedef compose_adaptor fit_rewritable_tag;
  131. BOOST_HOF_INHERIT_DEFAULT(compose_adaptor, detail::callable_base<F>)
  132. template<class X, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>)>
  133. constexpr compose_adaptor(X&& f1)
  134. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(detail::callable_base<F>, X&&)
  135. : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1))
  136. {}
  137. };
  138. template<class F1, class F2>
  139. struct compose_adaptor<F1, F2>
  140. : detail::compose_kernel<detail::callable_base<F1>, detail::callable_base<F2>>
  141. {
  142. typedef compose_adaptor fit_rewritable_tag;
  143. typedef detail::compose_kernel<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
  144. BOOST_HOF_INHERIT_CONSTRUCTOR(compose_adaptor, base_type)
  145. };
  146. BOOST_HOF_DECLARE_STATIC_VAR(compose, detail::make<compose_adaptor>);
  147. }} // namespace boost::hof
  148. #endif