double secArea(double theta, double radius){ return 0.5*theta*pow(radius,2); }
pow requires the header <cmath>. Angle is expected in radians.
auto papply = [](auto f, auto x) { return [=](auto y){ return f(x,y); }; };
auto op = papply(f, 6.28);
Outer lambda contains the argument known at the beginning, i.e. the specialized argument.
To achieve specialization we only have to pass the function and its first argument.
auto op = papply(secArea,2*M_PI); // full circle specialization auto val = op(3.0); // area of a circle with radius 3.0
double secArea(double rAngle, double radius);
double pow (double base, double exponent);
This application would produce 2 raised to any power.
auto op = papply(pow,2); // 2 raised to any power auto val = op(3); // 2^3 = 8
double pow (double base, double exponent);
auto swapArgs = [] (auto f){ return [=](auto x, auto y){ return f(y,x); }; };
auto op = papply(swapArgs(pow), 2); // raises to the power of 2 auto val = op(3); // 3^2 = 9, what we intendedThis solution only works for binary functions.
auto powS = [](auto exponent, auto base){ return pow(base, exponent); };
auto op = papply(powS, 2); // raises to the power of 2 auto val = op(3); // 3^2 = 9, what we intended
auto op = papply([](auto exponent, auto base){ return pow(base, exponent);}, 2); auto val = op(3); // 3^2 = 9, what we intended
This solution bypasses the use lambda expressions.
auto op = std::bind(pow, std::placeholders::_1, 2); auto val = op(3); // 3^2 = 9, what we intended
std::bind and std::placeholders require the header <functional>
double age(double remainingProportion, double halflife){ return log(remainingProportion)*halflife / -log(2); }
How old is a fossil that has 40% of C14 compared to a living sample?
auto val = op2(0.4);
bool std::regex_match( const std::string& s, const std::regex& re, std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
We are going to ignore the third parameter since it has a default value.
auto op = papply([](auto re, auto str){ return std::regex_match(str, re);}, std::regex("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"));
auto val1 = op("test@walletfox.com") // return 1, i.e. true auto val2 = op("test@walletfoxcom") // return 0, i.e. false
std::regex_match requires the header <regex>
double velocity(double v0, double a, double t){ return v0 + a*t; }
auto papply = [](auto f, auto... args) { return [=](auto... rargs) { return f(args..., rargs...); }; };The use of multiple arguments is expressed with a parameter pack ...
args refers to the primary specialization, rargs refers to the rest of the arguments.
auto op = papply(velocity, 0.0, 9.81); auto val = op(4.5); // returns 44.15 m/sIn this specific case, no swaps were necessary.
auto leftFold = [](auto col, auto op, auto init) { return std::accumulate(std::begin(col), std::end(col), init, op); };The function leftFold combines elements of the collection col, using a binary operation op, starting from the value init.
std::accumulate requires the header <numeric>
auto op = papply([](auto op, auto init, auto col){ return leftFold(col, op, init);}, std::plus<>(), 0.0);The function computes the sum of elements of the collection starting from the value 0.0.
std::plus requires the header <functional>
auto op = papply([](auto op, auto init, auto col){ return leftFold(col, op, init);}, __________, ___);