Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Church Numerals

Name: Anonymous 2013-09-22 17:16

What is this wizardry?


function zero(f) {
    return function (x) {
        return x;
    };
}

function succ(n) {
    return function (f) {
        return function (x) {
            return f(n(f)(x));
        };
    };
}

function multiplyBy2(x) {
    return x * 2;
}

console.log(zero(multiplyBy2)(1)); // => 1
console.log(succ(zero)(multiplyBy2)(1)); // => 2
console.log(succ(succ(zero))(multiplyBy2)(1)); // => 4
console.log(succ(succ(succ(zero)))(multiplyBy2)(1)); // => 8

Name: Anonymous 2013-09-23 13:27

>>6
Did you even read >>1's code? succ(succ(succ(zero))) would be hell to write without the currying.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List