Skip to content

Instantly share code, notes, and snippets.

@ahoka
Created February 23, 2016 22:12
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 ahoka/1eb08544c93491ec6461 to your computer and use it in GitHub Desktop.
Save ahoka/1eb08544c93491ec6461 to your computer and use it in GitHub Desktop.
#include <functional>
#include <iostream>
template <class A, class B, class C>
class Infix
{
private:
std::function<C(A, B)> funM;
public:
Infix(std::function<C(A, B)> fun)
: funM(fun)
{
}
class Partial
{
private:
std::function<C(B)> funM;
public:
Partial(std::function<C(A, B)> fun, A a)
{
funM = [a, fun] (B b) -> C {
return fun(a, b);
};
}
friend C
operator|(Partial p, B b)
{
return p.funM(b);
}
};
friend Partial
operator|(A a, Infix i)
{
return Partial(i.funM, a);
}
};
int
main()
{
Infix<int, int, int> add([](int a, int b) { return a + b; });
int val = 1 |add| 2;
std::cout << val << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment