4 ms·
I can answer both questions with a sprinkle of template magic: #include <iostream> #include <functional> template <typename Ret, typename First, t
by bheadmaster 3y ago
I can answer both questions with a sprinkle of template magic:
#include <iostream>
#include <functional>
template <typename Ret, typename First, typename ...Rest>
class Curry {
std::function<Ret(First, Rest...)> fn;
public:
Curry(std::function<Ret(First, Rest...)> fn): fn(fn) {};
Curry<Ret, Rest...> operator()(First arg) {
return Curry<Ret, Rest...>{[this, arg](Rest... args) {
return this->fn(arg, args...);
}};
}
};
template <typename Ret, typename Arg>
class Curry<Ret, Arg> {
std::function<Ret(Arg)> fn;
public:
Curry(std::function<Ret(Arg)> fn): fn(fn) {};
Ret operator()(Arg arg) {
return this->fn(arg);
}
};
int uncurried(int a, int b, int c) {
return a + 2*b + 3*c;
}
int main() {
auto curried = Curry<int, int, int, int>{uncurried};
std::cout << curried(1)(2)(3) << std::endl;
}
- whilenot-dev 3y agoI'm speechless, thank you very much! =)