-
-
Save drvink/cf89c578f3d5c84f004e to your computer and use it in GitHub Desktop.
function composition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
template <typename f1, typename f2> | |
struct compose_ { | |
f1 fun1; | |
f2 fun2; | |
template <typename... as> | |
auto operator()(as&&... args) -> | |
decltype(fun1(fun2(std::forward<as>(args)...))) | |
{ | |
return fun1(fun2(std::forward<as>(args)...)); | |
}; | |
}; | |
template <typename f1, typename f2> | |
compose_<f1, f2> | |
compose(f1&& fun1, f2&& fun2) | |
{ | |
return { std::forward<f1>(fun1), std::forward<f2>(fun2) }; | |
} | |
static int | |
add1(int x) | |
{ | |
return x + 1; | |
} | |
static int | |
add2(int x) | |
{ | |
return x + 2; | |
} | |
int | |
main() | |
{ | |
auto f = compose(add1, add2); | |
return f(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment