https://github.com/mozilla/gecko-dev
Raw File
Tip revision: d4f831862a6928b62a9fc427d200712d8cbc14e4 authored by Christian Legnitto on 16 September 2011, 02:55:42 UTC
Backout bug 678588 due to the volume regression in bug 598774, a=LegNeato
Tip revision: d4f8318
Y.js
// The Y combinator, applied to the factorial function

function factorial(proc) {
    return function (n) {
        return (n <= 1) ? 1 : n * proc(n-1);
    }
}

function Y(outer) {
    function inner(proc) {
        function apply(arg) {
            return proc(proc)(arg);
        }
        return outer(apply);
    }
    return inner(inner);
}

print("5! is " + Y(factorial)(5));
back to top