https://github.com/mozilla/gecko-dev
Raw File
Tip revision: 5ec6bde46c13b6d963d165fdb20f2e8f3a58460c authored by ffxbld on 04 January 2013, 23:31:18 UTC
Added FENNEC_18_0_RELEASE FENNEC_18_0_BUILD1 tag(s) for changeset 220ff6610655. DONTBUILD CLOSED TREE a=release
Tip revision: 5ec6bde
Y.js
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// 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