Last active
August 29, 2015 14:07
-
-
Save mfukar/3214a140a5a3b35c59fa to your computer and use it in GitHub Desktop.
C++ generic lambdas turn it into Lisp!
This file contains 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
/** | |
* Compile with GCC 4.9: | |
* g++ -std=c++1y lisp.cpp | |
*/ | |
#include <iostream> | |
#include <stdio.h> | |
auto terminal = [] (auto stream) { | |
return [=] (auto func) { | |
return func (stream); | |
}; | |
}; | |
auto curry = [] (auto f) { | |
return [=] (auto x) { | |
return [=] (auto y) { | |
return f(x, y); | |
}; | |
}; | |
}; | |
auto fmap = [] (auto f) { | |
return [=] (auto t) { | |
return terminal (f (t)); | |
}; | |
}; | |
int main (int argc, char **argv) | |
{ | |
auto plus = [] (auto a, auto b) { | |
return a + b; | |
}; | |
std::cout << curry(plus) (2) (3) << std::endl; | |
auto hello = [] (auto s) { | |
fprintf(s, "Hello "); return s; | |
}; | |
auto world = [] (auto s) { | |
fprintf(s, "world "); return s; | |
}; | |
auto endl = [] (auto s) { | |
fprintf(s, "\n"); return s; | |
}; | |
terminal (stdout) | |
(fmap(hello)) | |
(fmap(world)) | |
(fmap(endl)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment