Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created June 24, 2011 00:40
Show Gist options
  • Save michaelficarra/1043989 to your computer and use it in GitHub Desktop.
Save michaelficarra/1043989 to your computer and use it in GitHub Desktop.
fixed-point (Y) combinator in JS
// compute factorial of 5
Y(function(self){
return function(x){
return x == 0 ? 1 : x * self(x - 1);
};
})(5); // === 120
var Y = function(f){
return function recurse(){
return f(function(){ return recurse().apply(this, arguments); });
}();
};
var Y = function(f){
var recurse = function(self){
return f(function(){ return self(self).apply(this, arguments); });
};
return recurse(recurse);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment