Skip to content

Instantly share code, notes, and snippets.

auto map = [] (const auto &function, const auto &container )
{
auto retval = container;
retval.clear();
for (const auto &elem : container)
retval.push_back(function(elem));
return retval;
};
vector<int> data {1,2,3,4};
auto triple = [](auto a) { return a*3; };
cout << (triple * incr *dbl)(5) << endl;
auto incr = [](auto a) {return a+1;};
auto dbl = [](auto a) {return a*2;};
auto output_decorate = [](auto a) {cout<<a<<" "<<endl; return a;};
auto dbl_incr_decor = output_decorate * incr * dbl;
int x = dbl_incr_decor(5);
double y = dbl_incr_decor(.5);
auto lambda_plus = [] (auto x, auto y) { return x+y; };
int z = lambda_plus(5,3);
@lshort
lshort / bad_templates
Created May 24, 2014 15:02
A template conundrum
#include <string>
#include <iostream>
#include <functional>
using std::cout;
using std::endl;
template<typename return_type, typename ...params>
auto _L( return_type (*fp) (params... args))
{ return [fp] (params... args) { return fp(args...); }; };
@lshort
lshort / Parking Lot
Last active January 3, 2016 07:09
A bit of code I wrote to get back up to speed on C++
using namespace std;
using boost::optional;
// Implements a parking lot utility: you can ask it for an available space for
// a car of a certain size, ask it to find a car's parking spot given its license
// plate, ask what car is parked in a specific spot, etc.
// This code is organized top down: first the parking_lot class, then
// the distance class ('dist') and some functions to compare IEEE doubles, then
// the structs 'car' and 'parking_space'. I made some assumptions, like that each