The Currying class is a C++ template designed to implement the functional programming concept of currying. It allows for partial application of function arguments, creating a new function with some arguments preset and others to be provided later.
- Flexible Argument Handling: Supports currying of functions with various argument types, including support for ownership control of arguments.
- Ownership Control: Optional template parameter to specify whether the class should take ownership of the arguments.
- Reference Qualifier Overloads: Overloaded
operator()for both lvalue and rvalue references, allowing for optimized handling of temporary and non-temporary objects.
- C++20 or later (for
requires,constexpr if, and other modern C++ features).
Here's a simple example of how to use the Currying class:
#include "Currying.hpp"
int add(int a, int b) {
return a + b;
}
int main() {
auto curriedAdd = currying::Currying(add, currying::own_arguments, 5);
int result = curriedAdd(10); // result is 15
return 0;
}In this example, curriedAdd is a curried version of the add function with the first argument preset to 5.
You can control the ownership of the arguments:
ownArguments: TheCurryingclass takes ownership of the arguments.referenceArguments: TheCurryingclass does not take ownership of the arguments.
auto curriedFunction = currying::Currying(add, currying::referenceArguments, 5);Currying(FunctionType&& function, ArgOwnership<B> ownership, Args&&... arguments): Constructs aCurryingobject with the given function, ownership control, and arguments.
operator()(Args&&... args) &: Invokes the curried function with additional arguments for an lvalueCurryingobject.operator()(Args&&... args) &&: Invokes the curried function with additional arguments for an rvalueCurryingobject.getFunction() const: Returns the internal function.getArguments() const: Returns the internally stored arguments.
argCount() noexcept: Returns the total number of arguments expected by the function.argsFilled() noexcept: Returns the number of arguments already provided.argsLeft() noexcept: Returns the number of arguments still required.