https://github.com/mozilla/gecko-dev
Raw File
Tip revision: a616b6dc4b1f81525d747ecc40fb12981897fb3b authored by ffxbld on 01 February 2012, 23:13:12 UTC
Added tag FENNEC_11_0b1_BUILD1 for changeset 53451b6a6f9d. CLOSED TREE a=release
Tip revision: a616b6d
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