Skip to content

Instantly share code, notes, and snippets.

@cblp
Created August 2, 2017 15:27
Show Gist options
  • Save cblp/26402491a83b2cc84a2ec0e77b058a42 to your computer and use it in GitHub Desktop.
Save cblp/26402491a83b2cc84a2ec0e77b058a42 to your computer and use it in GitHub Desktop.
#include <functional>
#include <iostream>
using namespace std;
void cond(bool condition, function<void()> ifTrue, function<void()> ifFalse) {
function<void()> branches[] = {ifFalse, ifTrue};
branches[condition]();
}
template <typename T>
void enumerateDo(T start, T step, T stop, void action(int)) {
cond(
start <= stop,
[=]{
action(start);
enumerateDo(start + step, step, stop, action);
},
[]{}
);
}
int main() {
enumerateDo(3, 2, 10, [](int x){cout << x << ' ';});
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment