Reducing a multiple arguments function with currying
Currying is a technique to split a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. In other words, we create other functions based on a current function by reducing the current function. Let's suppose we have a function named areaOfRectangle()
, which takes two parameters, length
and width
. The code will be like this:
/* curry_1.cpp */ #include <functional> #include <iostream> using namespace std; // Variadic template for currying template<typename Func, typename... Args> auto curry(Func func, Args... args) { return [=](auto... lastParam) { return func(args..., lastParam...); }; } int areaOfRectangle(int length, int width) { return length * width; } auto main() -> int { cout << "[curry_1.cpp]" << endl; // Currying the areaOfRectangle() function...