Created
March 26, 2015 13:39
-
-
Save palladin/5515f45bbf62dee2c746 to your computer and use it in GitHub Desktop.
Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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