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 2:42

>>1

This looks like a convoluted solution to an already solved problem. Also, is there any particular reason why you are currying here? Javascript isn't Haskell, and allows you to use functions with multiple arguments. Unless you need to use it with map or something, you shouldn't use currying. It makes your code less readable.

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