var Y = function(proc) { return (function(x) { return proc(function(y) { return (x(x))(y);}); })(function(x) { return proc(function(y) { return (x(x))(y);}); }); }; var factgen = function(fact) { return function(n) { return (n === 0) ? 1 : n * fact(n-1); // calls argument, not self }; }; var fibgen = function(fib) { // this naive solution has exponential runtime complexity return function(n) { return (n <= 2) ? 1 : fib(n-1) + fib(n-2); }; }; console.log( Y(factgen)(5) ); // returns 120, i.e., 1 * 2 * 3 * 4 * 5 console.log( Y(fibgen)(7) ); // returns 13, i.e., the 7th fibonacci number var factorial = Y(factgen); // built entirely with anonymous functions var fibonacci = Y(fibgen); console.log( factorial(6) ); // 120 console.log([1,2,3,4,5,6,7,8,9,10].map( fibonacci) ); // the first 10 fibonacci numbers console.log( fibonacci(35) ); // uh oh, already getting kind of slow due to poor algorithm