-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposition.cpp
More file actions
52 lines (40 loc) · 1.33 KB
/
composition.cpp
File metadata and controls
52 lines (40 loc) · 1.33 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
49
50
51
52
// C++17 functional composition
// compiler explorer link https://godbolt.org/z/KyyG5p
#include <type_traits>
#include <functional>
#include <iostream>
template<typename F1, typename F2>
constexpr auto compose_helper(F1&& f1, F2&& f2) {
// capture by value and hoping that the compiler will optimize this away
return [f1, f2](auto&& ...args)
constexpr {
return f2((f1)(std::forward<decltype(args)>(args)...));
};
}
// operator sadly does not work
template<typename F1, typename F2>
constexpr auto operator>>(F1&& f1, F2&& f2) {
return compose(std::forward<F1>(f1), std::forward<F2>(f2));
}
template<typename F1, typename... Fns>
constexpr auto compose(F1&& f1, Fns&& ... fns) {
if constexpr (sizeof...(fns) == 1) {
return compose_helper(std::forward<F1>(f1), std::forward<Fns>(fns)...);
}
else {
return compose_helper(std::forward<F1>(f1), compose(std::forward<Fns>(fns)...));
}
}
constexpr int add3(int a, int b, int c) noexcept {
return a + b + c;
}
template<typename T>
constexpr auto id(T&& t) noexcept {
return std::forward<T>(t);
}
int main() {
// type of a genetic function has to be specified, however generic lambda is allowed
constexpr auto fn = compose(add3, [](auto i) { return i * 2; }, id<int>);
constexpr auto res = fn(10, 20, 30);
std::cout << res;
}