Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created March 8, 2017 17:25
Show Gist options
  • Save marty1885/b41b494aea1c168cd9cbf4b8f610d20d to your computer and use it in GitHub Desktop.
Save marty1885/b41b494aea1c168cd9cbf4b8f610d20d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <functional>
#include <type_traits>
using namespace std;
auto If = [](bool condition, auto tFunc, auto fFunc)
{
function<void()> funcTable[2] = {fFunc,tFunc};
funcTable[(int)condition]();
};
template<typename Func>
class Iterate : Func
{
public:
Iterate(Func func) : Func(move(func)){}
protected:
using Func::operator();
public:
void operator()(int current, int end)
{
If(current+1 != end,
[&](){
operator()(current);
operator()(current-1,end);
},
[](){});
}
};
auto ForRange = [](int current, int end, auto func)
{
using Func = decltype(func);
Iterate<decay_t<Func>>it(forward<Func>(func));
it.operator()(current,end);
};
void p(int a,int b)
{
If(a<b,[&](){
swap(a,b);
},[](){}
);
ForRange(a,b,[](auto v){cout << v << endl;});
}
int main()
{
p(5,1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment