-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurring.cpp
More file actions
48 lines (42 loc) · 1.43 KB
/
curring.cpp
File metadata and controls
48 lines (42 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// C++17 curring
// compiler explorer link https://godbolt.org/z/y8pkIg
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <cstddef>
#include <functional>
template<typename F>
constexpr auto curry(F&& f) {
// I din't find any simple way to avoid by-value capture that works for all cases
// thankfully compilers are able to inline all of this madness quite easily
return [f](auto&& ... args) constexpr {
if constexpr (!std::is_invocable_v<F, decltype(args)...>) {
return curry([&f, &args...](auto&& ... xs)
constexpr -> std::invoke_result_t<F, decltype(args)..., decltype(xs)...> {
return f(std::forward<decltype(args)>(args)..., std::forward<decltype(xs)>(xs)...);
});
}
else {
return f(std::forward<decltype(args)>(args)...);
}
};
}
constexpr int add3(const int a, const int b, const int c) {
return a + b + c;
}
int main() {
// note: most IDEs get confused about this type of code
constexpr auto fn = curry(add3);
constexpr auto res1 = fn(10, 20)(30);
constexpr auto res2 = fn(10)(20, 30);
constexpr auto res3 = fn(10, 20)(30);
constexpr auto res4 = fn(10)(20)(30);
constexpr auto res5 = fn(10, 20, 30);
std::cout << res1 << '\n';
std::cout << res2 << '\n';
std::cout << res3 << '\n';
std::cout << res4 << '\n';
std::cout << res5 << '\n';
return 0;
}