-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrz_fp.hh
191 lines (162 loc) · 5.35 KB
/
crz_fp.hh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef __CRZ_FP_HH__
#define __CRZ_FP_HH__
#include <type_traits>
#include <functional>
#include <tuple>
namespace crz {
namespace __detail {
template<typename R, typename ...As>
struct __function_traits_base {
using function_type = std::function<R(As...)>;
using result_type = R;
using argument_types = std::tuple<As...>;
};
template<typename F>
struct __function_traits;
template<typename F>
struct __function_traits<std::reference_wrapper<F>> : public __function_traits<F> {};
template<typename R, typename ...As>
struct __function_traits<R(*)(As...)> : public __function_traits_base<R, As...> {};
template<typename R, typename C, typename ...As>
struct __function_traits<R(C::*)(As...)> : public __function_traits_base<R, As...> {};
template<typename R, typename C, typename ...As>
struct __function_traits<R(C::*)(As...) const> : public __function_traits_base<R, As...> {};
template<typename F>
struct __function_traits : public __function_traits<decltype(&F::operator())> {};
}
namespace fp {
template<typename F>
struct function_traits : public __detail::__function_traits<std::decay_t<F>> {};
}
namespace __detail {
template<typename T>
auto __copy_or_move(const T &t) -> T {
if constexpr (std::is_copy_constructible_v<T>) {
return t;
} else {
return std::move(const_cast<T &>(t));
}
}
template<typename F, typename T1, typename T2>
class __curry_cacher;
template<typename F, typename TA, typename A, typename ...As>
class __curry_cacher<F, TA, std::tuple<A, As...>> {
F f;
TA cached_args;
public:
__curry_cacher(F f, TA args) : f(std::move(f)), cached_args(std::move(args)) {}
auto operator()(A arg) {
auto new_cached_args = std::tuple_cat(
__copy_or_move(cached_args),
std::tuple<A>(std::forward<A>(arg)));
return __curry_cacher<F,
decltype(new_cached_args),
std::tuple<As...>>(__copy_or_move(f), std::move(new_cached_args));
}
};
template<typename F, typename TA, typename A>
class __curry_cacher<F, TA, std::tuple<A>> {
F f;
TA cached_args;
public:
__curry_cacher(F f, TA args) : f(std::move(f)), cached_args(std::move(args)) {}
auto operator()(A arg) {
return std::apply(f, std::tuple_cat(
__copy_or_move(cached_args),
std::tuple<A>(std::forward<A>(arg))));
}
};
template<typename F, typename T1, typename T2>
class __partial_cacher;
template<typename F, typename TA, typename ...As>
class __partial_cacher<F, TA, std::tuple<As...>> {
F f;
TA cached_args;
public:
__partial_cacher(F f, TA args) : f(std::move(f)), cached_args(std::move(args)) {}
auto operator()(As... args) {
return std::apply(f, std::tuple_cat(
__copy_or_move(cached_args),
std::tuple<As...>(std::forward<As>(args)...)));
}
};
template<std::size_t I, typename T, typename = void>
struct __tuple_drop_n;
template<std::size_t I, typename T>
using __tuple_drop_n_t = typename __tuple_drop_n<I, T>::type;
template<typename ...Ts>
struct __tuple_drop_n<0, std::tuple<Ts...>> {
using type = std::tuple<Ts...>;
};
template<std::size_t I, typename T, typename ...Ts>
struct __tuple_drop_n<I, std::tuple<T, Ts...>, std::enable_if_t<(I > 0)>> {
using type = __tuple_drop_n_t<I - 1, std::tuple<Ts...>>;
};
}
namespace fp {
template<typename F>
auto curry(F f) {
using arg_types = typename function_traits<F>::argument_types;
if constexpr (std::tuple_size_v<arg_types> < 2) {
return f;
} else {
return __detail::__curry_cacher<F, std::tuple<>, arg_types>
(std::move(f), std::tuple<>());
}
}
template<typename F, typename ...As>
auto partial(F f, As &&...args) {
using arg_types = typename function_traits<F>::argument_types;
static_assert(sizeof...(As) <= std::tuple_size_v<arg_types>, "Too many arguments");
if constexpr (sizeof...(As) == 0) {
return f;
} else if constexpr (sizeof...(As) == std::tuple_size_v<arg_types>) {
return f(std::forward<As>(args)...);
} else {
return __detail::__partial_cacher<F, std::tuple<As...>, __detail::__tuple_drop_n_t<sizeof...(As), arg_types>>
(std::move(f), std::tuple<As...>(std::forward<As>(args)...));
}
}
template<typename F>
auto pipe(F f) {
return f;
}
template<typename F, typename ...Fs>
auto pipe(F f, Fs ...fs) {
return [=, f = std::move(f)](auto &&...args) {
return pipe(fs...)(f(std::forward<decltype(args)>(args)...));
};
}
template<typename F>
auto compose(F f) {
return f;
}
template<typename F, typename ...Fs>
auto compose(F f, Fs ...fs) {
return [=, f = std::move(f)](auto &&...args) {
return f(compose(fs...)(std::forward<decltype(args)>(args)...));
};
}
template<typename F1, typename F2, typename J>
auto fork_join(F1 f1, F2 f2, J join) {
return [f1 = std::move(f1), f2 = std::move(f2), join = std::move(join)](auto &&...args) {
return join(f1(std::forward<decltype(args)>(args)...),
f2(std::forward<decltype(args)>(args)...));
};
}
template<typename T>
auto identity(T &&t) {
return std::forward<T>(t);
}
template<typename T>
auto seq(T &&t) {
return std::forward<T>(t);
}
template<typename T, typename F, typename ...Fs>
auto seq(T &&t, F f, Fs ...fs) {
f(std::forward<T>(t));
return seq(std::forward<T>(t), fs...);
}
}
}
#endif // __CRZ_FP_HH__