https://github.com/mozilla/gecko-dev
Raw File
Tip revision: a2b76b0564f163d3a1ba93e3b55a2fba1305d39b authored by tbirdbld on 20 June 2012, 03:39:11 UTC
Added THUNDERBIRD_14_0b3_RELEASE THUNDERBIRD_14_0b3_BUILD1 tag(s) for changeset 7945471fc07f. DONTBUILD CLOSED TREE a=release
Tip revision: a2b76b0
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