3 Defines `boost::hana::curry`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_FUNCTIONAL_CURRY_HPP
11 #define BOOST_HANA_FUNCTIONAL_CURRY_HPP
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/detail/decay.hpp>
15 #include <boost/hana/functional/apply.hpp>
16 #include <boost/hana/functional/partial.hpp>
19 #include <type_traits>
23 BOOST_HANA_NAMESPACE_BEGIN
24 //! @ingroup group-functional
25 //! Curry a function up to the given number of arguments.
27 //! [Currying][Wikipedia.currying] is a technique in which we consider a
28 //! function taking multiple arguments (or, equivalently, a tuple of
29 //! arguments), and turn it into a function which takes a single argument
30 //! and returns a function to handle the remaining arguments. To help
31 //! visualize, let's denote the type of a function `f` which takes
32 //! arguments of types `X1, ..., Xn` and returns a `R` as
34 //! (X1, ..., Xn) -> R
37 //! Then, currying is the process of taking `f` and turning it into an
38 //! equivalent function (call it `g`) of type
40 //! X1 -> (X2 -> (... -> (Xn -> R)))
43 //! This gives us the following equivalence, where `x1`, ..., `xn` are
44 //! objects of type `X1`, ..., `Xn` respectively:
46 //! f(x1, ..., xn) == g(x1)...(xn)
49 //! Currying can be useful in several situations, especially when working
50 //! with higher-order functions.
52 //! This `curry` utility is an implementation of currying in C++.
53 //! Specifically, `curry<n>(f)` is a function such that
55 //! curry<n>(f)(x1)...(xn) == f(x1, ..., xn)
58 //! Note that the `n` has to be specified explicitly because the existence
59 //! of functions with variadic arguments in C++ make it impossible to know
60 //! when currying should stop.
62 //! Unlike usual currying, this implementation also allows a curried
63 //! function to be called with several arguments at a time. Hence, the
64 //! following always holds
66 //! curry<n>(f)(x1, ..., xk) == curry<n - k>(f)(x1)...(xk)
69 //! Of course, this requires `k` to be less than or equal to `n`; failure
70 //! to satisfy this will trigger a static assertion. This syntax is
71 //! supported because it makes curried functions usable where normal
72 //! functions are expected.
74 //! Another "extension" is that `curry<0>(f)` is supported: `curry<0>(f)`
75 //! is a nullary function; whereas the classical definition for currying
76 //! seems to leave this case undefined, as nullary functions don't make
77 //! much sense in purely functional languages.
82 //! @include example/functional/curry.cpp
85 //! [Wikipedia.currying]: http://en.wikipedia.org/wiki/Currying
86 #ifdef BOOST_HANA_DOXYGEN_INVOKED
87 template <std::size_t n>
88 constexpr auto curry = [](auto&& f) {
89 return [perfect-capture](auto&& x1) {
90 return [perfect-capture](auto&& x2) {
92 return [perfect-capture](auto&& xn) -> decltype(auto) {
94 forwarded(x1), forwarded(x2), ..., forwarded(xn)
101 template <std::size_t n, typename F>
104 template <std::size_t n>
105 struct make_curry_t {
106 template <typename F>
107 constexpr curry_t<n, typename detail::decay<F>::type>
108 operator()(F&& f) const { return {static_cast<F&&>(f)}; }
111 template <std::size_t n>
112 constexpr make_curry_t<n> curry{};
114 namespace curry_detail { namespace {
115 template <std::size_t n>
116 constexpr make_curry_t<n> curry_or_call{};
119 constexpr auto curry_or_call<0> = apply;
122 template <std::size_t n, typename F>
126 template <typename ...X>
127 constexpr decltype(auto) operator()(X&& ...x) const& {
128 static_assert(sizeof...(x) <= n,
129 "too many arguments provided to boost::hana::curry");
130 return curry_detail::curry_or_call<n - sizeof...(x)>(
131 partial(f, static_cast<X&&>(x)...)
135 template <typename ...X>
136 constexpr decltype(auto) operator()(X&& ...x) & {
137 static_assert(sizeof...(x) <= n,
138 "too many arguments provided to boost::hana::curry");
139 return curry_detail::curry_or_call<n - sizeof...(x)>(
140 partial(f, static_cast<X&&>(x)...)
144 template <typename ...X>
145 constexpr decltype(auto) operator()(X&& ...x) && {
146 static_assert(sizeof...(x) <= n,
147 "too many arguments provided to boost::hana::curry");
148 return curry_detail::curry_or_call<n - sizeof...(x)>(
149 partial(std::move(f), static_cast<X&&>(x)...)
154 template <typename F>
155 struct curry_t<0, F> {
158 constexpr decltype(auto) operator()() const&
161 constexpr decltype(auto) operator()() &
164 constexpr decltype(auto) operator()() &&
165 { return std::move(f)(); }
168 BOOST_HANA_NAMESPACE_END
170 #endif // !BOOST_HANA_FUNCTIONAL_CURRY_HPP