-
Notifications
You must be signed in to change notification settings - Fork 0
/
curry.h
73 lines (57 loc) · 1.81 KB
/
curry.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef CURRY_H_INCLUDED
#define CURRY_H_INCLUDED
#include <functional>
#include <type_traits>
template <typename T>
class CurriedFunction;
template <typename R, typename... Args>
class CurriedFunction<R(Args...)>
{
private:
std::function<R(Args...)> func;
template <size_t N, typename... Arguments>
struct DropArgumentsHelper;
template <size_t N, typename T, typename... Arguments>
struct DropArgumentsHelper<N, T, Arguments...>
{
using type = std::conditional_t<
N <= 0,
CurriedFunction<R(T, Arguments...)>,
typename DropArgumentsHelper<N - 1, Arguments...>::type
>;
};
template <size_t N>
struct DropArgumentsHelper<N>
{
using type = void;
};
template <size_t N>
using DropArguments = typename DropArgumentsHelper<N, Args...>::type;
enum class Enabler {};
static constexpr Enabler dummyEnabler = {};
public:
CurriedFunction() = default;
template <typename F>
CurriedFunction(const F& f): func{f} {}
template <typename F>
CurriedFunction& operator=(const F& f)
{
func = f;
return *this;
}
template <typename... CallArgs, std::enable_if_t<(sizeof...(CallArgs) == sizeof...(Args)), Enabler> = dummyEnabler>
auto operator()(CallArgs &&... args)
{
return func(std::forward<CallArgs>(args)...);
}
template <typename... CallArgs, std::enable_if_t<(sizeof...(CallArgs) < sizeof...(Args)), Enabler> = dummyEnabler>
auto operator()(CallArgs &&... args)
{
return DropArguments<sizeof...(CallArgs)> {
[funcCopy = func, &args...](auto &&... rest) mutable {
return funcCopy(std::forward<CallArgs>(args)..., std::forward<decltype(rest)>(rest)...);
}
};
}
};
#endif // CURRY_H_INCLUDED