https://github.com/mozilla/gecko-dev
Raw File
Tip revision: 31bad8966ecad45e1b9496842ca9b074906460ec authored by ffxbld on 28 June 2012, 12:38:40 UTC
Added FENNEC_14_0b10_RELEASE FENNEC_14_0b10_BUILD1 tag(s) for changeset 997811a777df. DONTBUILD CLOSED TREE a=release
Tip revision: 31bad89
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