Skip to content

Instantly share code, notes, and snippets.

@Masstronaut
Created October 20, 2015 13:56
Show Gist options
  • Save Masstronaut/f4e9c74e36d6318eca0d to your computer and use it in GitHub Desktop.
Save Masstronaut/f4e9c74e36d6318eca0d to your computer and use it in GitHub Desktop.
Using lambdas to generate lambdas.
#include <iostream>
#include <cassert>
#include <cmath>
int main()
{
auto add = [](auto x) { return [x](auto y) { return x + y; }; };
auto pyth = [](auto x) { return [x](auto y) { return std::sqrt(x * x + y * y); }; };
// are lambdas really fucking dumb?!?!
auto add4 = add(4);
auto addhalf = add(0.5);
auto val6 = add4(2);
auto val10 = add4(6);
auto val15 = addhalf(1);
auto pyth3 = pyth(3);
auto val5 = pyth3(4);
assert(val6 == 6);
assert(val10 == 10);
assert(val15 == 1.5);
assert(val5 == 5);
std::cout << "success!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment