Created
July 8, 2012 17:00
-
-
Save kakkun61/3071774 to your computer and use it in GitHub Desktop.
Functional Composition in C++ ver. 1
This file contains hidden or 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<iostream> | |
#include<functional> | |
#include<cmath> | |
using namespace std; | |
using namespace std::tr1; | |
template<typename Param, typename Middle, typename Result> | |
function<Result(Param)> compose(const function<Result(Middle)>& f, const function<Middle(Param)>& g) { | |
return [f, g](Param x){return f(g(x));}; | |
} | |
int main(void) { | |
function<double(double)> f = [](double x){return exp(x);}; | |
function<double(double)> g = [](double x){return pow(x, 2);}; | |
function<double(double)> h = [](double x){return -x + 1;}; | |
function<double(double)> composed = compose(compose(f, g), h); | |
cout << composed(3) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment