]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::curry`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
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) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_FUNCTIONAL_CURRY_HPP | |
11 | #define BOOST_HANA_FUNCTIONAL_CURRY_HPP | |
12 | ||
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> | |
17 | ||
18 | #include <cstddef> | |
19 | #include <type_traits> | |
20 | #include <utility> | |
21 | ||
22 | ||
23 | BOOST_HANA_NAMESPACE_BEGIN | |
24 | //! @ingroup group-functional | |
25 | //! Curry a function up to the given number of arguments. | |
26 | //! | |
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 | |
33 | //! @code | |
34 | //! (X1, ..., Xn) -> R | |
35 | //! @endcode | |
36 | //! | |
37 | //! Then, currying is the process of taking `f` and turning it into an | |
38 | //! equivalent function (call it `g`) of type | |
39 | //! @code | |
40 | //! X1 -> (X2 -> (... -> (Xn -> R))) | |
41 | //! @endcode | |
42 | //! | |
43 | //! This gives us the following equivalence, where `x1`, ..., `xn` are | |
44 | //! objects of type `X1`, ..., `Xn` respectively: | |
45 | //! @code | |
46 | //! f(x1, ..., xn) == g(x1)...(xn) | |
47 | //! @endcode | |
48 | //! | |
49 | //! Currying can be useful in several situations, especially when working | |
50 | //! with higher-order functions. | |
51 | //! | |
52 | //! This `curry` utility is an implementation of currying in C++. | |
53 | //! Specifically, `curry<n>(f)` is a function such that | |
54 | //! @code | |
55 | //! curry<n>(f)(x1)...(xn) == f(x1, ..., xn) | |
56 | //! @endcode | |
57 | //! | |
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. | |
61 | //! | |
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 | |
65 | //! @code | |
66 | //! curry<n>(f)(x1, ..., xk) == curry<n - k>(f)(x1)...(xk) | |
67 | //! @endcode | |
68 | //! | |
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. | |
73 | //! | |
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. | |
78 | //! | |
79 | //! | |
80 | //! Example | |
81 | //! ------- | |
82 | //! @include example/functional/curry.cpp | |
83 | //! | |
84 | //! | |
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) { | |
91 | ... | |
92 | return [perfect-capture](auto&& xn) -> decltype(auto) { | |
93 | return forwarded(f)( | |
94 | forwarded(x1), forwarded(x2), ..., forwarded(xn) | |
95 | ); | |
96 | }; | |
97 | }; | |
98 | }; | |
99 | }; | |
100 | #else | |
101 | template <std::size_t n, typename F> | |
102 | struct curry_t; | |
103 | ||
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)}; } | |
109 | }; | |
110 | ||
111 | template <std::size_t n> | |
112 | constexpr make_curry_t<n> curry{}; | |
113 | ||
114 | namespace curry_detail { | |
115 | template <std::size_t n> | |
116 | constexpr make_curry_t<n> curry_or_call{}; | |
117 | ||
118 | template <> | |
119 | constexpr auto curry_or_call<0> = apply; | |
120 | } | |
121 | ||
122 | template <std::size_t n, typename F> | |
123 | struct curry_t { | |
124 | F f; | |
125 | ||
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)...) | |
132 | ); | |
133 | } | |
134 | ||
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)...) | |
141 | ); | |
142 | } | |
143 | ||
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)...) | |
150 | ); | |
151 | } | |
152 | }; | |
153 | ||
154 | template <typename F> | |
155 | struct curry_t<0, F> { | |
156 | F f; | |
157 | ||
158 | constexpr decltype(auto) operator()() const& | |
159 | { return f(); } | |
160 | ||
161 | constexpr decltype(auto) operator()() & | |
162 | { return f(); } | |
163 | ||
164 | constexpr decltype(auto) operator()() && | |
165 | { return std::move(f)(); } | |
166 | }; | |
167 | #endif | |
168 | BOOST_HANA_NAMESPACE_END | |
169 | ||
170 | #endif // !BOOST_HANA_FUNCTIONAL_CURRY_HPP |