Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created October 27, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfsternberg/abd213af8ad30625e4592f9b9b774bcd to your computer and use it in GitHub Desktop.
Save elfsternberg/abd213af8ad30625e4592f9b9b774bcd to your computer and use it in GitHub Desktop.
#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