Skip to content

Instantly share code, notes, and snippets.

@palladin
Created March 26, 2015 13:39
Show Gist options
  • Save palladin/5515f45bbf62dee2c746 to your computer and use it in GitHub Desktop.
Save palladin/5515f45bbf62dee2c746 to your computer and use it in GitHub Desktop.
Streams
#include <iostream>
using namespace std;
int foo()
{
auto flatMap = [](auto f, auto s) { return [=](auto k) { return s([=](auto x) { f(x)(k); }); }; };
auto map = [](auto f, auto s) { return [=](auto k) { s([=](auto x) { k(f(x)); }); }; };
auto filter = [](auto p, auto s) { return [=](auto k) { s([=](auto x) { if(p(x)) k(x); }); }; };
auto sum = [](auto k) { int sum = 0; k([&](int x) { sum += x; }); return sum; };
auto length = [](auto k) { int count = 0; k([&](int x) { count++; }); return count; };
auto source = [](auto k) { for(int i = 0; i <= 35; i++) k(i); };
auto result = sum(map([](int x) { return x + 1; }, source));
return result;
}
int main()
{
cout << foo() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment