Orchid Win7 v1 <voi### [at] dev null> wrote:
> 1. You can take a 5-argument function, pass it 2 argument, and get a
> 3-argument function as the result.
Would this be currying in C++?
std::function<int(int)> multiplierFunction(int multiplier)
{
return [multiplier](int value) { return value * multiplier; };
}
The above function returns a function that takes an integral as parameter
and returns it multiplied by the specified multiplier. To better understand
it, it's used eg. like this:
auto doubler = multiplierFunction(2);
auto tripler = multiplierFunction(3);
auto nMultiplier = multiplierFunction(n);
std::cout << doubler(5); // Will print "10"
std::cout << tripler(5); // Will print "15"
std::cout << nMultiplier(5); // Will print the result of 5*n
--
- Warp
Post a reply to this message
|