Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Last active December 20, 2015 04:29
Show Gist options
  • Select an option

  • Save PatrickJS/6071667 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/6071667 to your computer and use it in GitHub Desktop.
Y Combinator in Javascript
var y = function(le) {
return function(f) {
return f(f);
}(function(f) {
return le(
function(x) {
return (f(f))(x);
}
);
});
};
var factorial = y(function(fac) {
return function(n) {
return n <= 2 ? n : n * fac(n - 1);
};
});
/*
var yo = function(f) {
return le(function(x) {
return (f(f))(x);
});
};
var y = function(le) {
return function(f) {
return f(f);
}(yo);
};
var factorial = y(function(fac) {
return function(n) {
if (n <= 2) {
return n;
} else {
return n * fac(n - 1);
}
};
});
*/
var number120 = factorial(5);
number120 === 120;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment