https://github.com/mozilla/gecko-dev
Raw File
Tip revision: 958e6d9bcc78482a0774e941bfb1253ef628364b authored by seabld on 15 June 2012, 11:54:31 UTC
Added tag SEAMONKEY_2_10_1_RELEASE for changeset FIREFOX_13_0_1_BUILD1. CLOSED TREE a=release
Tip revision: 958e6d9
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