Last active
May 5, 2016 12:36
-
-
Save ecatmur/b4e2179a7e9bb47f50d6499f576473f7 to your computer and use it in GitHub Desktop.
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
// Pure Z combinator in C++14 variadic polymorphic lambdas. | |
auto fix = [](auto&& f) { | |
return [&](auto&& x) { | |
return x(x); }([f = static_cast<decltype(f)&&>(f)](auto& x) mutable { | |
return [&](auto&&... a) -> decltype(auto) { | |
return f([&](auto&&... b) -> decltype(auto) { | |
return x(x)(static_cast<decltype(b)&&>(b)...); }, | |
static_cast<decltype(a)&&>(a)...); }; }); }; | |
#include <iostream> | |
#include <tuple> | |
int main() { | |
std::cout << fix([](auto f, int n) -> int { return n < 2 ? n : n * f(n - 1); })(5) << std::endl; | |
fix([](auto f, auto&& a, auto&&... b) { | |
std::cout << std::forward<decltype(a)>(a); | |
std::get<sizeof...(b) == 0>(std::make_tuple( | |
[&](auto&&... b) { | |
std::cout << ' '; | |
f(std::forward<decltype(b)>(b)...); }, | |
[]{}))(std::forward<decltype(b)>(b)...); })("hello", "world!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment