curry.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*!
  2. @file
  3. Defines `boost::hana::curry`.
  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_FUNCTIONAL_CURRY_HPP
  9. #define BOOST_HANA_FUNCTIONAL_CURRY_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/detail/decay.hpp>
  12. #include <boost/hana/functional/apply.hpp>
  13. #include <boost/hana/functional/partial.hpp>
  14. #include <cstddef>
  15. #include <type_traits>
  16. #include <utility>
  17. BOOST_HANA_NAMESPACE_BEGIN
  18. //! @ingroup group-functional
  19. //! Curry a function up to the given number of arguments.
  20. //!
  21. //! [Currying][Wikipedia.currying] is a technique in which we consider a
  22. //! function taking multiple arguments (or, equivalently, a tuple of
  23. //! arguments), and turn it into a function which takes a single argument
  24. //! and returns a function to handle the remaining arguments. To help
  25. //! visualize, let's denote the type of a function `f` which takes
  26. //! arguments of types `X1, ..., Xn` and returns a `R` as
  27. //! @code
  28. //! (X1, ..., Xn) -> R
  29. //! @endcode
  30. //!
  31. //! Then, currying is the process of taking `f` and turning it into an
  32. //! equivalent function (call it `g`) of type
  33. //! @code
  34. //! X1 -> (X2 -> (... -> (Xn -> R)))
  35. //! @endcode
  36. //!
  37. //! This gives us the following equivalence, where `x1`, ..., `xn` are
  38. //! objects of type `X1`, ..., `Xn` respectively:
  39. //! @code
  40. //! f(x1, ..., xn) == g(x1)...(xn)
  41. //! @endcode
  42. //!
  43. //! Currying can be useful in several situations, especially when working
  44. //! with higher-order functions.
  45. //!
  46. //! This `curry` utility is an implementation of currying in C++.
  47. //! Specifically, `curry<n>(f)` is a function such that
  48. //! @code
  49. //! curry<n>(f)(x1)...(xn) == f(x1, ..., xn)
  50. //! @endcode
  51. //!
  52. //! Note that the `n` has to be specified explicitly because the existence
  53. //! of functions with variadic arguments in C++ make it impossible to know
  54. //! when currying should stop.
  55. //!
  56. //! Unlike usual currying, this implementation also allows a curried
  57. //! function to be called with several arguments at a time. Hence, the
  58. //! following always holds
  59. //! @code
  60. //! curry<n>(f)(x1, ..., xk) == curry<n - k>(f)(x1)...(xk)
  61. //! @endcode
  62. //!
  63. //! Of course, this requires `k` to be less than or equal to `n`; failure
  64. //! to satisfy this will trigger a static assertion. This syntax is
  65. //! supported because it makes curried functions usable where normal
  66. //! functions are expected.
  67. //!
  68. //! Another "extension" is that `curry<0>(f)` is supported: `curry<0>(f)`
  69. //! is a nullary function; whereas the classical definition for currying
  70. //! seems to leave this case undefined, as nullary functions don't make
  71. //! much sense in purely functional languages.
  72. //!
  73. //!
  74. //! Example
  75. //! -------
  76. //! @include example/functional/curry.cpp
  77. //!
  78. //!
  79. //! [Wikipedia.currying]: http://en.wikipedia.org/wiki/Currying
  80. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  81. template <std::size_t n>
  82. constexpr auto curry = [](auto&& f) {
  83. return [perfect-capture](auto&& x1) {
  84. return [perfect-capture](auto&& x2) {
  85. ...
  86. return [perfect-capture](auto&& xn) -> decltype(auto) {
  87. return forwarded(f)(
  88. forwarded(x1), forwarded(x2), ..., forwarded(xn)
  89. );
  90. };
  91. };
  92. };
  93. };
  94. #else
  95. template <std::size_t n, typename F>
  96. struct curry_t;
  97. template <std::size_t n>
  98. struct make_curry_t {
  99. template <typename F>
  100. constexpr curry_t<n, typename detail::decay<F>::type>
  101. operator()(F&& f) const { return {static_cast<F&&>(f)}; }
  102. };
  103. template <std::size_t n>
  104. constexpr make_curry_t<n> curry{};
  105. namespace curry_detail { namespace {
  106. template <std::size_t n>
  107. constexpr make_curry_t<n> curry_or_call{};
  108. template <>
  109. constexpr auto curry_or_call<0> = apply;
  110. }}
  111. template <std::size_t n, typename F>
  112. struct curry_t {
  113. F f;
  114. template <typename ...X>
  115. constexpr decltype(auto) operator()(X&& ...x) const& {
  116. static_assert(sizeof...(x) <= n,
  117. "too many arguments provided to boost::hana::curry");
  118. return curry_detail::curry_or_call<n - sizeof...(x)>(
  119. partial(f, static_cast<X&&>(x)...)
  120. );
  121. }
  122. template <typename ...X>
  123. constexpr decltype(auto) operator()(X&& ...x) & {
  124. static_assert(sizeof...(x) <= n,
  125. "too many arguments provided to boost::hana::curry");
  126. return curry_detail::curry_or_call<n - sizeof...(x)>(
  127. partial(f, static_cast<X&&>(x)...)
  128. );
  129. }
  130. template <typename ...X>
  131. constexpr decltype(auto) operator()(X&& ...x) && {
  132. static_assert(sizeof...(x) <= n,
  133. "too many arguments provided to boost::hana::curry");
  134. return curry_detail::curry_or_call<n - sizeof...(x)>(
  135. partial(std::move(f), static_cast<X&&>(x)...)
  136. );
  137. }
  138. };
  139. template <typename F>
  140. struct curry_t<0, F> {
  141. F f;
  142. constexpr decltype(auto) operator()() const&
  143. { return f(); }
  144. constexpr decltype(auto) operator()() &
  145. { return f(); }
  146. constexpr decltype(auto) operator()() &&
  147. { return std::move(f)(); }
  148. };
  149. #endif
  150. BOOST_HANA_NAMESPACE_END
  151. #endif // !BOOST_HANA_FUNCTIONAL_CURRY_HPP