https://github.com/mozilla/gecko-dev
Raw File
Tip revision: 735e768d0b6231997d95cf9ecc43a1fb3c1faafc authored by ffxbld on 13 December 2011, 01:58:11 UTC
Added tag FENNEC_9_0b6_BUILD1 for changeset 541d40f07d00. CLOSED TREE a=release
Tip revision: 735e768
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