https://github.com/mozilla/gecko-dev
Raw File
Tip revision: f23ca81fc750fb4dbaa3cdad78d5f0378706ba8e authored by seabld on 29 April 2012, 08:09:23 UTC
Added tag SEAMONKEY_2_9_1_RELEASE for changeset FIREFOX_12_0_BUILD1. CLOSED TREE a=release
Tip revision: f23ca81
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