Save all modification
[mozilla-1.9/m8.git] / js / src / Y.js
blobe92a65a5df977952002fe7b3186d94d3acfb6b41
1 // The Y combinator, applied to the factorial function
3 function factorial(proc) {
4     return function (n) {
5         return (n <= 1) ? 1 : n * proc(n-1);
6     }
9 function Y(outer) {
10     function inner(proc) {
11         function apply(arg) {
12             return proc(proc)(arg);
13         }
14         return outer(apply);
15     }
16     return inner(inner);
19 print("5! is " + Y(factorial)(5));