Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active June 27, 2016 02:04
Show Gist options
  • Save alphaKAI/6d68d6b7343ddb4639025a6f303b2dd3 to your computer and use it in GitHub Desktop.
Save alphaKAI/6d68d6b7343ddb4639025a6f303b2dd3 to your computer and use it in GitHub Desktop.
Z Combinator for D and C++
#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;
}
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