Open tylerjw opened 2 years ago
Pasting here incase the compiler-explorer link ever goes dead:
#include <iostream>
template <typename Callable, typename... Params>
auto curry(Callable f, Params... ps)
{
if constexpr (requires { f(ps...); }) {
return f(ps...);
} else {
return [f, ps...] (auto... qs)
{
return curry(f, ps..., qs...);
};
}
}
int func1(int x, int y)
{
return x + y;
}
int main()
{
const auto f = curry(func1);
std::cout << f(1)(2) << '\n';
return 0;
}
Need
In order to call functions that have multiple inputs in a monadic way As a user I want to have nice syntax for curing a function
Scenario
Scenario: fp should provide a curry function GIVEN A function with multiple inputs WHEN I curry it with this new Curried type THEN I should have a function that can be called with a single argument.
Reference: https://en.wikipedia.org/wiki/Currying
Tasks
[ ] What: Search for and read existing implementations of Currying in C++ Why: To understand how it might be implemented and what is difficult about Currying Impact: Implementing Currying should be more straightforward
[ ] What: Write a test and example to show how the interface will work Why: To understand what a nice interface will look like Impact: Currying interface will be better understood
[ ] What: Create the type to curry functions Why: To have this feature Impact: Test will pass