Skip to content

Instantly share code, notes, and snippets.

@meki
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save meki/807f10cc35f72932c0b4 to your computer and use it in GitHub Desktop.

Select an option

Save meki/807f10cc35f72932c0b4 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <type_traits>
namespace colls {
template <typename C, typename F>
C for_each(const C& c, F unop)
{
C out;
std::copy(std::begin(c), std::end(c), std::back_inserter(out));
std::for_each(std::begin(out), std::end(out), unop);
return std::move(out);
}
template <typename C_out, typename C, typename F>
C_out for_each(const C& c, F unop)
{
C_out out;
std::copy(c.begin(), c.end(), std::back_inserter(out));
std::for_each(out.begin(), out.end(), unop);
return std::move(out);
}
template <typename C, typename F>
C map(const C& coll, F unop)
{
C out = coll;
std::for_each(out.begin(), out.end(), unop);
return std::move(out);
}
template <typename C, typename F>
C filter(const C& coll, F unop)
{
C out;
std::for_each(coll.begin(), coll.end(), [&](const auto& e) {
if(unop(e))
{
std::back_inserter(out) = e;
}
});
return std::move(out);
}
template <typename C, typename F>
auto reduceLeft(const C& c, F biop)
{
typedef typename std::decay<decltype(c.front())>::type E;
auto out = E();
if(c.empty()) { return out; }
auto r = c.begin();
out += *r;
++r;
while(r != c.end())
{
out = biop(out, *r);
++r;
}
return out;
}
}
#include <iostream>
#include "immutable.hpp"
#include <vector>
#include <list>
#include <string>
#include <cassert>
using namespace std;
int main(void){
using namespace colls;
auto testMap = [&]() {
vector<int> v = {1, 2, 3, 5, 10, 100 };
auto v2 = map(v, [](auto& a){ a *= 2; });
for_each(v2.begin(), v2.end(), [](auto& a){ cout << a << ", "; });
cout << endl;
int v3[] = {1, 2, 3, 5, 10, 100 };
auto v4 = map(v3, [](auto& a) {a *= 2; });
for_each(begin(v4), end(v4), [](auto a) {cout << a << ", "; } );
cout << endl;
};
testMap();
auto testFilter = [&]() {
vector<int> v = {1, 2, 3, 5, 10, 100 };
auto v2 = filter(v, [](auto& a){ return a % 2 == 0; });
for_each(v2.begin(), v2.end(), [](auto& a){ cout << a << ", "; });
cout << endl;
};
testFilter();
auto testReduceLeft = [&]() {
vector<int> v = {1, 2, 3, 5, 10, 100 };
auto add = [](const auto& l, const auto& r) { return l + r; };
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << "reduce v1 = " << reduceLeft(v1, add ) << endl;
vector<string> v2 = {"this ", "is ", "a ", "pen."};
cout << "reduce v2 = " << reduceLeft(v2, add ) << endl;
vector<double> v3;
cout << "reduce v3 = " << reduceLeft(v3, add) << endl;
};
testReduceLeft();
auto testForEach = [&]() {
vector<int> v = {1, 2, 3, 5, 10, 100 };
auto l = for_each<std::list<int>>(v, [](auto& a) { ++a; } );
for(auto a : l) { cout << a << ", "; }
cout << endl;
auto l2 = for_each(v, [](auto& a) { ++a; } );
for(auto a : l2) { cout << a << ", "; }
cout << endl;
};
testForEach();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment