https://github.com/mozilla/gecko-dev
Raw File
Tip revision: d78328a1f2f6d150891e2da6efa4d62339a734b5 authored by seabld on 17 September 2011, 00:52:06 UTC
Added tag SEAMONKEY_2_4b3_RELEASE for changeset FIREFOX_7_0b6_BUILD1. CLOSED TREE a=release
Tip revision: d78328a
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