https://github.com/mozilla/gecko-dev
Raw File
Tip revision: 2c4f51be60ad9c560d8ea89ff3e7aaaec06192b7 authored by seabld on 31 May 2012, 07:30:18 UTC
Added tag SEAMONKEY_2_10b3_RELEASE for changeset FIREFOX_13_0b6_BUILD1. CLOSED TREE a=release
Tip revision: 2c4f51b
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