Skip to content

Instantly share code, notes, and snippets.

@argv0
Created November 4, 2013 22:20
Show Gist options
  • Save argv0/7310192 to your computer and use it in GitHub Desktop.
Save argv0/7310192 to your computer and use it in GitHub Desktop.
#include <map>
#include <vector>
#include <string>
#include <future>
#include <numeric>
#include <iterator>
#include <boost/lexical_cast.hpp>
using namespace std;
using boost::lexical_cast;
typedef map<string, string> object;
vector<object> objects = {
{{"user", "kyle"}, {"amount", "42"}},
{{"user", "kyle"}, {"amount", "1"}},
{{"user", "andy"}, {"amount", "0"}}
};
namespace arg {
template <typename T, typename Pred>
vector<T> remove_copy_if(vector<T> t, Pred f)
{
vector<T> res;
std::remove_copy_if(begin(t), end(t), back_inserter(res), f);
return res;
}
template <typename T, typename F>
auto transform(vector<T> t, F f) -> vector<decltype(f(T()))>
{
vector<decltype(f(T()))> res;
std::transform(begin(t), end(t), back_inserter(res), f);
return res;
}
template <typename T>
int accumulate(vector<T> t)
{
return std::accumulate(begin(t), end(t), 0);
}
}
int main(void) {
cout << arg::accumulate(
arg::transform(
arg::remove_copy_if(objects,[](object o){ return o["user"] != "kyle"; }),
[](object o) { return lexical_cast<int>(o["amount"]); })) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment