Last active
June 27, 2016 02:04
-
-
Save alphaKAI/6d68d6b7343ddb4639025a6f303b2dd3 to your computer and use it in GitHub Desktop.
Z Combinator for D and C++
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> | |
#include <functional> | |
template <typename R, typename... Args> | |
std::function<R(Args...)> Z(std::function<R(std::function<R(Args...)>, Args...)> f) { | |
return [&](const Args&... args) { return f(Z(f), args...); }; | |
} | |
int main(int argc, char const* argv[]) { | |
std::cout << | |
Z( | |
(std::function<long(std::function<long(long)>, long)>) | |
[](std::function<long(long)> factorial, long n) { | |
return n <= 1 ? 1 : n * factorial(n - 1); | |
} | |
)(5) | |
<< std::endl; | |
} |
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
import std.stdio; | |
R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f){ | |
return (Args args) => f(Z(f), args); | |
} | |
void main(){ | |
writeln(Z((ulong delegate(ulong) factorial, ulong n) => n <= 1 ? 1 : n * factorial(n - 1))(5)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment