Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'Currying'?

I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!)

like image 309
Ben Avatar asked Aug 30 '08 20:08

Ben


People also ask

What is Currying explain with example?

Currying is a technique of evaluating function with multiple arguments, into sequence of functions with single argument.In other words, when a function, instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third ...

What is Currying in functional programming?

Currying is the transformation of a function with multiple arguments into a sequence of single-argument functions. That means converting a function like this f(a, b, c, ...) into a function like this f(a)(b)(c)... . If you know Python's functools. partial , then this is very similar to it.

What is meant by Currying in JavaScript?

Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript: function add (a, b) { return a + b; } add(3, 4); // returns 7. This is a function that takes two arguments, a and b, and returns their sum.

What is Currying in JavaScript and its benefits?

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.


2 Answers

Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript:

function add (a, b) {   return a + b; }  add(3, 4); // returns 7 

This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function:

function add (a) {   return function (b) {     return a + b;   } } 

This is a function that takes one argument, a, and returns a function that takes another argument, b, and that function returns their sum.

add(3)(4);  var add3 = add(3);  add3(4); 

The first statement returns 7, like the add(3, 4) statement. The second statement defines a new function called add3 that will add 3 to its argument. (This is what some may call a closure.) The third statement uses the add3 operation to add 3 to 4, again producing 7 as a result.

like image 86
Kyle Cronin Avatar answered Sep 28 '22 03:09

Kyle Cronin


In an algebra of functions, dealing with functions that take multiple arguments (or equivalent one argument that's an N-tuple) is somewhat inelegant -- but, as Moses Schönfinkel (and, independently, Haskell Curry) proved, it's not needed: all you need are functions that take one argument.

So how do you deal with something you'd naturally express as, say, f(x,y)? Well, you take that as equivalent to f(x)(y) -- f(x), call it g, is a function, and you apply that function to y. In other words, you only have functions that take one argument -- but some of those functions return other functions (which ALSO take one argument;-).

As usual, wikipedia has a nice summary entry about this, with many useful pointers (probably including ones regarding your favorite languages;-) as well as slightly more rigorous mathematical treatment.

like image 23
Alex Martelli Avatar answered Sep 28 '22 02:09

Alex Martelli