curry.hpp 5.5 KB

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