Created
October 27, 2016 15:44
-
-
Save elfsternberg/abd213af8ad30625e4592f9b9b774bcd to your computer and use it in GitHub Desktop.
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> | |
typedef std::function<int(int)> Addable; | |
int addem(int start) { | |
std::cout << "Add: " << start << std::endl; | |
return start + 10; | |
} | |
int subem(int start) { | |
std::cout << "Sub: " << start << std::endl; | |
return start - 5; | |
} | |
/* This is somewhat silly and pointless, but it really | |
* does help me visualize how the new functional programming | |
* features in C++ work. | |
*/ | |
int main() { | |
Addable addable = addem; | |
int start = 5; | |
start = addable(5); | |
for(int i = 0; i < 20; i++) { | |
addable = (i % 2 == 0) ? addem : subem; | |
start = addable(start); | |
} | |
std::cout << "Done: " << start << std::endl; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment