Skip to content

Instantly share code, notes, and snippets.

@ripter
Created May 25, 2012 05:11
Show Gist options
  • Save ripter/2785877 to your computer and use it in GitHub Desktop.
Save ripter/2785877 to your computer and use it in GitHub Desktop.
Curry Method

#What is the difference between Curry and Partial? This seems like a confusing topic for a lot of people. I actually had a really hard time trying to understand the difference between the two. A lot of people think they are the same thing, or that currying is just a special form of partial application.

##So what is currying? When you curry a function, it returns a function that calls another function for every parameter. Currying creates a series of single parameter functions. Some code examples should clear this up.

function test(a, b, c) {
    return 'a: ' + a + ', b: ' + b + ', c: ' + c;
}

var c = curry(test);
c(10)(20)(30);

// so c looks something like like:
function c(a) {
    return function(b) {
        return function(c) {
            test(a, b, c);
        }
    }
}

This is different from a Partial because a partial only returns a single function instead of a chain of functions. Here is what a partial would look like.

function test(a, b, c) {
    return 'a: ' + a + ', b: ' + b + ', c: ' + c;
}

var p = partial(test, 10);
p(30, 40);

// so p looks something like:
function p(b, c) {
    return test(10, b, c);
}

##Conclusion I'm not sure how currying would be useful in Javascript, so I'm not going to make any attempt to write a curry method. If you want one, Functional JavaScript provides the curry method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment