Created
June 24, 2011 00:40
-
-
Save michaelficarra/1043989 to your computer and use it in GitHub Desktop.
fixed-point (Y) combinator in JS
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
// compute factorial of 5 | |
Y(function(self){ | |
return function(x){ | |
return x == 0 ? 1 : x * self(x - 1); | |
}; | |
})(5); // === 120 |
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
var Y = function(f){ | |
return function recurse(){ | |
return f(function(){ return recurse().apply(this, arguments); }); | |
}(); | |
}; |
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
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