https://github.com/mozilla/gecko-dev
Raw File
Tip revision: c09734ecd5aa14ff174464b13e7429efd328fba6 authored by ffxbld on 24 August 2011, 23:45:06 UTC
Added tag FIREFOX_7_0b2_BUILD1 for changeset 4ee096ccdd5a. CLOSED TREE a=release
Tip revision: c09734e
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