Y Combinator in JavaScript
Y Combinator is a venture firm that funds startups, they have had some success with Reddit and some other companies. The thing I really dig is their name and in this blog post I'll try to describe what Y Combinator is.
On Wikipedia the Y Combinator article begins like this:
An easier way to understand the Y combinator is that we can create recursive functions without explicit support for recursion, we only need high-order functions to have recursion... Now I am going to show you how this is possible in JavaScript. The factorial function can be defined like this: function fac(n) {
return n == 0 ? 1 : n * fac(n - 1);
}
alert(fac(5)); //120
As you see it calls itself recursively. But we can also implement factorial without calling fac explicitly: function facMaker(fn) {
return function(n) {
return n == 0 ? 1 : n * fn(fn)(n - 1);
}
}
var fac1 = facMaker(facMaker);
alert(fac1(5)); //120
Two things to notice:
This is what facMaker(facMaker) really does: var fac2 = function(fn) {
return function(n) {
return n == 0 ? 1 : n * fn(fn)(n - 1);
}}(function(fn) {
return function(n) {
return n == 0 ? 1 : n * fn(fn)(n - 1);
}});
alert(fac2(5)); //120
This rewrite trick is essentially what a Y Combinator does, i.e. a Y Combinator takes a function and turns it in a recursive function! Y Combinator implemented in JavaScript looks like this: function Y(dn) {
return function(fn) {
return fn(fn);
}(function(fn) {
return dn(function(x) {
return fn(fn)(x);
});
});
}
And more generally like this: function Y(dn) {
return function(fn) {
return fn(fn);
}(function(fn) {
return dn(function() {
return fn(fn).apply(null, arguments);
});
});
}
We can implement factorial like this using a Y Combinator: var fac3 = Y(function(fn) {
return function(n) {
return n == 0 ? 1 : n * fn(n - 1);
}
});
Beautiful, but not that useful :)
Update:
Code
·
JavaScript
•
25. Aug 2007
Post a comment
Commenting on this post has expired.
|
Blog labels |