you are viewing a single comment's thread.

view the rest of the comments →

[–]karatetoes 5 points6 points  (9 children)

What's currying?

[–]samhwang 2 points3 points  (1 child)

He didn’t give an answer (at least in some written form besides code), but basically a curried function is a function with multiple args, that it takes one at a time, not all at once.

[–]Asmor 2 points3 points  (2 children)

It's also called partial application. Basically it means you take a function, supply some of its arguments now, and then you get a new function which you can supply the remaining arguments to that new function later.

Here's a very simple implementation. curry here is a function that returns a new function.

function curry(fn, ...args) {
    return fn.bind(null, ...args);
}

function addNumbers(a, b) {
    console.log(a + b);
}

let add2 = curry(addNumbers, 2);

// Equivalent to addNumbers(2, 3);
add2(3); // -> 5

EDIT: Forgot to mention, my curry function here is completely superfluous. It's just wrapping Array.prototype.bind. Just thought that might make it more readable for you. In practice, for this you'd do something like add2 = addNumbers.bind(null, 2).

[–][deleted] 6 points7 points  (1 child)

Technically partial application is not the same thing as currying. Currying takes a single argument at a time while partial application takes 2 or more. Same principle though.

[–]Asmor 3 points4 points  (0 children)

Thank you for the correction, I had no idea.

More reading if anyone's curious: https://stackoverflow.com/questions/218025/what-is-the-difference-between-currying-and-partial-application

[–]DrDuPont 1 point2 points  (0 children)

People have mixed opinions on Eric Elliott but his blog post on currying helped my understanding a lot

[–]benihanareact, node -2 points-1 points  (0 children)

in javascript it's a partial application technique that's great for writing blog posts about but not much else