<p>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!)</p>

[{'answer_id': 36321, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 11, 'selected': True, 'text': '<p>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:</p>\n\n<pre class="lang-js prettyprint-override"><code>function add (a, b) {\n  return a + b;\n}\n\nadd(3, 4); // returns 7\n</code></pre>\n<p>This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function:</p>\n<pre class="lang-js prettyprint-override"><code>function add (a) {\n  return function (b) {\n    return a + b;\n  }\n}\n</code></pre>\n<p>This is a function that takes one argument, <code>a</code>, and returns a function that takes another argument, <code>b</code>, and that function returns their sum.</p>\n<pre class="lang-js prettyprint-override"><code>add(3)(4); // returns 7\n\nvar add3 = add(3); // returns a function\n\nadd3(4); // returns 7\n</code></pre>\n<ul>\n<li>The first statement returns 7, like the <code>add(3, 4)</code> statement.</li>\n<li>The second statement defines a new function called <code>add3</code> that will\nadd 3 to its argument. (This is what some may call a closure.)</li>\n<li>The third statement uses the <code>add3</code> operation to add 3 to 4, again\nproducing 7 as a result.</li>\n</ul>\n'}, {'answer_id': 36340, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 6, 'selected': False, 'text': '<p>Currying is a transformation that can be applied to functions to allow them to take one less argument than previously.</p>\n\n<p>For example, in F# you can define a function thus:-</p>\n\n\n\n<pre class="lang-ml prettyprint-override"><code>let f x y z = x + y + z\n</code></pre>\n\n<p>Here function f takes parameters x, y and z and sums them together so:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>f 1 2 3\n</code></pre>\n\n<p>Returns 6.</p>\n\n<p>From our definition we can can therefore define the curry function for f:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let curry f = fun x -&gt; f x\n</code></pre>\n\n<p>Where \'fun x -> f x\' is a lambda function equivilent to x => f(x) in C#. This function inputs the function you wish to curry and returns a function which <em>takes a single argument</em> and returns the specified function with the first argument set to the input argument.</p>\n\n<p>Using our previous example we can obtain a curry of f thus:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let curryf = curry f\n</code></pre>\n\n<p>We can then do the following:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let f1 = curryf 1\n</code></pre>\n\n<p>Which provides us with a function f1 which is equivilent to f1 y z = 1 + y + z. This means we can do the following:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>f1 2 3\n</code></pre>\n\n<p>Which returns 6.</p>\n\n<p>This process is often confused with \'partial function application\' which can be defined thus:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let papply f x = f x\n</code></pre>\n\n<p>Though we can extend it to more than one parameter, i.e.:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let papply2 f x y = f x y\nlet papply3 f x y z = f x y z\netc.\n</code></pre>\n\n<p>A partial application will take the function and parameter(s) and return a function that requires one or more less parameters, and as the previous two examples show is implemented directly in the standard F# function definition so we could achieve the previous result thus:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let f1 = f 1\nf1 2 3\n</code></pre>\n\n<p>Which will return a result of 6.</p>\n\n<p>In conclusion:-</p>\n\n<p>The difference between currying and partial function application is that:-</p>\n\n<p>Currying takes a function and provides a new function accepting a single argument, and returning the specified function with its first argument set to that argument. <em>This allows us to represent functions with multiple parameters  as a series of single argument functions</em>. Example:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let f x y z = x + y + z\nlet curryf = curry f\nlet f1 = curryf 1\nlet f2 = curryf 2\nf1 2 3\n6\nf2 1 3\n6\n</code></pre>\n\n<p>Partial function application is more direct - it takes a function and one or more arguments and returns a function with the first n arguments set to the n arguments specified. Example:-</p>\n\n<pre class="lang-ml prettyprint-override"><code>let f x y z = x + y + z\nlet f1 = f 1\nlet f2 = f 2\nf1 2 3\n6\nf2 1 3\n6\n</code></pre>\n'}, {'answer_id': 216055, 'author': 'J D', 'author_id': 13924, 'author_profile': 'https://Stackoverflow.com/users/13924', 'pm_score': 5, 'selected': False, 'text': '<p>A curried function is a function of several arguments rewritten such that it accepts the first argument and returns a function that accepts the second argument and so on. This allows functions of several arguments to have some of their initial arguments partially applied.</p>\n'}, {'answer_id': 1352865, 'author': 'Alex Martelli', 'author_id': 95810, 'author_profile': 'https://Stackoverflow.com/users/95810', 'pm_score': 7, 'selected': False, 'text': '<p>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.</p>\n\n<p>So how do you deal with something you\'d naturally express as, say, <code>f(x,y)</code>?  Well, you take that as equivalent to <code>f(x)(y)</code> -- <code>f(x)</code>, call it <code>g</code>, is a function, and you apply that function to <code>y</code>.  In other words, you only have functions that take one argument -- but some of those functions return other functions (which ALSO take one argument;-).</p>\n\n<p>As usual, <a href="http://en.wikipedia.org/wiki/Currying">wikipedia</a> 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.</p>\n'}, {'answer_id': 1352892, 'author': 'James Black', 'author_id': 67566, 'author_profile': 'https://Stackoverflow.com/users/67566', 'pm_score': 2, 'selected': False, 'text': '<p>I found this article, and the article it references, useful, to better understand currying:\n<a href="http://blogs.msdn.com/wesdyer/archive/2007/01/29/currying-and-partial-function-application.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/wesdyer/archive/2007/01/29/currying-and-partial-function-application.aspx</a></p>\n\n<p>As the others mentioned, it is just a way to have a one parameter function.</p>\n\n<p>This is useful in that you don\'t have to assume how many parameters will be passed in, so you don\'t need a 2 parameter, 3 parameter and 4 parameter functions.</p>\n'}, {'answer_id': 1352898, 'author': 'Shea Daniels', 'author_id': 125372, 'author_profile': 'https://Stackoverflow.com/users/125372', 'pm_score': 7, 'selected': False, 'text': '<p>Here\'s a concrete example:</p>\n\n<p>Suppose you have a function that calculates the gravitational force acting on an object. If you don\'t know the formula, you can find it <a href="http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation" rel="noreferrer">here</a>. This function takes in the three necessary parameters as arguments.</p>\n\n<p>Now, being on the earth, you only want to calculate forces for objects on this planet. In a functional language, you could pass in the mass of the earth to the function and then partially evaluate it. What you\'d get back is another function that takes only two arguments and calculates the gravitational force of objects on earth. This is called currying.</p>\n'}, {'answer_id': 1354439, 'author': 'Anon', 'author_id': 108445, 'author_profile': 'https://Stackoverflow.com/users/108445', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s a toy example in Python:</p>\n\n\n\n<pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; from functools import partial as curry\n\n&gt;&gt;&gt; # Original function taking three parameters:\n&gt;&gt;&gt; def display_quote(who, subject, quote):\n        print who, \'said regarding\', subject + \':\'\n        print \'"\' + quote + \'"\'\n\n\n&gt;&gt;&gt; display_quote("hoohoo", "functional languages",\n           "I like Erlang, not sure yet about Haskell.")\nhoohoo said regarding functional languages:\n"I like Erlang, not sure yet about Haskell."\n\n&gt;&gt;&gt; # Let\'s curry the function to get another that always quotes Alex...\n&gt;&gt;&gt; am_quote = curry(display_quote, "Alex Martelli")\n\n&gt;&gt;&gt; am_quote("currying", "As usual, wikipedia has a nice summary...")\nAlex Martelli said regarding currying:\n"As usual, wikipedia has a nice summary..."\n</code></pre>\n\n<p>(Just using concatenation via + to avoid distraction for non-Python programmers.)</p>\n\n<p>Editing to add:</p>\n\n<p>See <a href="http://docs.python.org/library/functools.html?highlight=partial#functools.partial" rel="nofollow noreferrer">http://docs.python.org/library/functools.html?highlight=partial#functools.partial</a>,\nwhich also shows the partial object vs. function distinction in the way Python implements this.</p>\n'}, {'answer_id': 23454424, 'author': 'catch23', 'author_id': 1498427, 'author_profile': 'https://Stackoverflow.com/users/1498427', 'pm_score': 2, 'selected': False, 'text': '<p>A curried function is applied to multiple argument lists, instead of just\none. </p>\n\n<p>Here is a regular, non-curried function, which adds two Int\nparameters, x and y:</p>\n\n<pre><code>scala&gt; def plainOldSum(x: Int, y: Int) = x + y\nplainOldSum: (x: Int,y: Int)Int\nscala&gt; plainOldSum(1, 2)\nres4: Int = 3\n</code></pre>\n\n<p>Here is similar function that’s curried. Instead\nof one list of two Int parameters, you apply this function to two lists of one\nInt parameter each:</p>\n\n<pre><code>scala&gt; def curriedSum(x: Int)(y: Int) = x + y\ncurriedSum: (x: Int)(y: Int)Intscala&gt; second(2)\nres6: Int = 3\nscala&gt; curriedSum(1)(2)\nres5: Int = 3\n</code></pre>\n\n<p>What’s happening here is that when you invoke <code>curriedSum</code>, you actually get two traditional function invocations back to back. The first function\ninvocation takes a single Int parameter named <code>x</code> , and returns a function\nvalue for the second function. This second function takes the Int parameter\n<code>y</code>. </p>\n\n<p>Here’s a function named <code>first</code> that does in spirit what the first traditional\nfunction invocation of <code>curriedSum</code> would do:</p>\n\n<pre><code>scala&gt; def first(x: Int) = (y: Int) =&gt; x + y\nfirst: (x: Int)(Int) =&gt; Int\n</code></pre>\n\n<p>Applying 1 to the first function—in other words, invoking the first function\nand passing in 1 —yields the second function:</p>\n\n<pre><code>scala&gt; val second = first(1)\nsecond: (Int) =&gt; Int = &lt;function1&gt;\n</code></pre>\n\n<p>Applying 2 to the second function yields the result:</p>\n\n<pre><code>scala&gt; second(2)\nres6: Int = 3\n</code></pre>\n'}, {'answer_id': 31081596, 'author': 'Mario', 'author_id': 25358, 'author_profile': 'https://Stackoverflow.com/users/25358', 'pm_score': 3, 'selected': False, 'text': "<p>If you understand <code>partial</code> you're halfway there.  The idea of <code>partial</code> is to preapply arguments to a function and give back a new function that wants only the remaining arguments.  When this new function is called it includes the preloaded arguments along with whatever arguments were supplied to it.</p>\n\n<p>In Clojure <code>+</code> is a function but to make things starkly clear:</p>\n\n<pre><code>(defn add [a b] (+ a b))\n</code></pre>\n\n<p>You may be aware that the <code>inc</code> function simply adds 1 to whatever number it's passed.</p>\n\n<pre><code>(inc 7) # =&gt; 8\n</code></pre>\n\n<p>Let's build it ourselves using <code>partial</code>:</p>\n\n<pre><code>(def inc (partial add 1))\n</code></pre>\n\n<p>Here we return another function that has 1 loaded into the first argument of <code>add</code>.  As <code>add</code> takes two arguments the new <code>inc</code> function wants only the <code>b</code> argument -- not 2 arguments as before since 1 has already been <em>partially</em> applied.  Thus <code>partial</code> is a tool from which to create new functions with default values presupplied.  That is why in a functional language functions often order arguments from general to specific.  This makes it easier to reuse such functions from which to construct other functions.</p>\n\n<p>Now imagine if the language were smart enough to understand introspectively that <code>add</code> wanted two arguments.  When we passed it one argument, rather than balking, what if the function partially applied the argument we passed it on our behalf understanding that we probably meant to provide the other argument later?  We could then define <code>inc</code> without explicitly using <code>partial</code>.</p>\n\n<pre><code>(def inc (add 1)) #partial is implied\n</code></pre>\n\n<p>This is the way some languages behave.  It is exceptionally useful when one wishes to compose functions into larger transformations.  This would lead one to transducers.</p>\n"}, {'answer_id': 34795493, 'author': 'Adzz', 'author_id': 3601621, 'author_profile': 'https://Stackoverflow.com/users/3601621', 'pm_score': 6, 'selected': False, 'text': '<p>It can be a way to use functions to make other functions. </p>\n\n<p>In javascript:</p>\n\n\n\n<pre class="lang-js prettyprint-override"><code>let add = function(x){\n  return function(y){ \n   return x + y\n  };\n};\n</code></pre>\n\n<p>Would allow us to call it like so:</p>\n\n<pre class="lang-js prettyprint-override"><code>let addTen = add(10);\n</code></pre>\n\n<p>When this runs the <code>10</code> is passed in as <code>x</code>;</p>\n\n<pre class="lang-js prettyprint-override"><code>let add = function(10){\n  return function(y){\n    return 10 + y \n  };\n};\n</code></pre>\n\n<p>which means we are returned this function:</p>\n\n<pre class="lang-js prettyprint-override"><code>function(y) { return 10 + y };\n</code></pre>\n\n<p>So when you call</p>\n\n<pre class="lang-js prettyprint-override"><code> addTen();\n</code></pre>\n\n<p>you are really calling:</p>\n\n<pre class="lang-js prettyprint-override"><code> function(y) { return 10 + y };\n</code></pre>\n\n<p>So if you do this:</p>\n\n<pre class="lang-js prettyprint-override"><code> addTen(4)\n</code></pre>\n\n<p>it\'s the same as:</p>\n\n<pre class="lang-js prettyprint-override"><code>function(4) { return 10 + 4} // 14\n</code></pre>\n\n<p>So our <code>addTen()</code> always adds ten to whatever we pass in. We can make similar functions in the same way:</p>\n\n<pre class="lang-js prettyprint-override"><code>let addTwo = add(2)       // addTwo(); will add two to whatever you pass in\nlet addSeventy = add(70)  // ... and so on...\n</code></pre>\n\n<p>Now the obvious follow up question is why on earth would you ever want to do that? It turns what was an eager operation <code>x + y</code> into one that can be stepped through lazily, meaning we can do at least two things \n 1. cache expensive operations\n 2. achieve abstractions in the functional paradigm.</p>\n\n<p>Imagine our curried function looked like this:</p>\n\n<pre class="lang-js prettyprint-override"><code>let doTheHardStuff = function(x) {\n  let z = doSomethingComputationallyExpensive(x)\n  return function (y){\n    z + y\n  }\n}\n</code></pre>\n\n<p>We could call this function once, then pass around the result to be used in lots of places, meaning we only do the computationally expensive stuff once:</p>\n\n<pre class="lang-js prettyprint-override"><code>let finishTheJob = doTheHardStuff(10)\nfinishTheJob(20)\nfinishTheJob(30)\n</code></pre>\n\n<p>We can get abstractions in a similar way.</p>\n'}, {'answer_id': 36541937, 'author': 'S2dent', 'author_id': 3664083, 'author_profile': 'https://Stackoverflow.com/users/3664083', 'pm_score': 2, 'selected': False, 'text': "<p>An example of currying would be when having functions you only know one of the parameters at the moment:</p>\n\n<p>For example:</p>\n\n<pre><code>func aFunction(str: String) {\n    let callback = callback(str) // signature now is `NSData -&gt; ()`\n    performAsyncRequest(callback)\n}\n\nfunc callback(str: String, data: NSData) {\n    // Callback code\n}\n\nfunc performAsyncRequest(callback: NSData -&gt; ()) {\n    // Async code that will call callback with NSData as parameter\n}\n</code></pre>\n\n<p>Here, since you don't know the second parameter for callback when sending it to <code>performAsyncRequest(_:)</code> you would have to create another lambda / closure to send that one to the function.</p>\n"}, {'answer_id': 49895155, 'author': 'user3804449', 'author_id': 3804449, 'author_profile': 'https://Stackoverflow.com/users/3804449', 'pm_score': 2, 'selected': False, 'text': '<p>As all other answers currying helps to create partially applied functions. Javascript does not provide native support for automatic currying. So the examples provided above may not help in practical coding. There is some excellent example in livescript (Which essentially compiles to js)\n<a href="http://livescript.net/" rel="nofollow noreferrer">http://livescript.net/</a></p>\n\n<pre><code>times = (x, y) --&gt; x * y\ntimes 2, 3       #=&gt; 6 (normal use works as expected)\ndouble = times 2\ndouble 5         #=&gt; 10\n</code></pre>\n\n<p>In above example when you have given less no of arguments livescript generates new curried function for you (double) </p>\n'}, {'answer_id': 50962077, 'author': 'Marcus Thornton', 'author_id': 2288882, 'author_profile': 'https://Stackoverflow.com/users/2288882', 'pm_score': 2, 'selected': False, 'text': '<p>Curry can simplify your code. This is one of the main reasons to use this. Currying is a process of converting a function that accepts n arguments into n functions that accept only one argument.</p>\n\n<p>The principle is to pass the arguments of the passed function, using the closure (closure) property, to store them in another function and treat it as a return value, and these functions form a chain, and the final arguments are passed in to complete the operation.</p>\n\n<p>The benefit of this is that it can simplify the processing of parameters by dealing with one parameter at a time, which can also improve the flexibility and readability of the program. This also makes the program more manageable. Also dividing the code into smaller pieces would make it reuse-friendly. </p>\n\n<p>For example:</p>\n\n<pre><code>function curryMinus(x) \n{\n  return function(y) \n  {\n    return x - y;\n  }\n}\n\nvar minus5 = curryMinus(1);\nminus5(3);\nminus5(5);\n</code></pre>\n\n<p>I can also do...</p>\n\n<pre><code>var minus7 = curryMinus(7);\nminus7(3);\nminus7(5);\n</code></pre>\n\n<p>This is very great for making complex code neat and handling of unsynchronized methods etc.</p>\n'}, {'answer_id': 52355185, 'author': 'V. S.', 'author_id': 10014202, 'author_profile': 'https://Stackoverflow.com/users/10014202', 'pm_score': 1, 'selected': False, 'text': '<p>Here you can find a simple explanation of currying implementation in C#. In the comments, I have tried to show how currying can be useful:</p>\n\n<pre><code>public static class FuncExtensions {\n    public static Func&lt;T1, Func&lt;T2, TResult&gt;&gt; Curry&lt;T1, T2, TResult&gt;(this Func&lt;T1, T2, TResult&gt; func)\n    {\n        return x1 =&gt; x2 =&gt; func(x1, x2);\n    }\n}\n\n//Usage\nvar add = new Func&lt;int, int, int&gt;((x, y) =&gt; x + y).Curry();\nvar func = add(1);\n\n//Obtaining the next parameter here, calling later the func with next parameter.\n//Or you can prepare some base calculations at the previous step and then\n//use the result of those calculations when calling the func multiple times \n//with different input parameters.\n\nint result = func(1);\n</code></pre>\n'}, {'answer_id': 55357631, 'author': 'MidhunKrishna', 'author_id': 3485581, 'author_profile': 'https://Stackoverflow.com/users/3485581', 'pm_score': 3, 'selected': False, 'text': '<p>Currying is translating a function from callable as <code>f(a, b, c)</code> into callable as <code>f(a)(b)(c)</code>. </p>\n\n<p>Otherwise currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments.</p>\n\n<p>Literally, currying is a transformation of functions: from one way of calling into another. In JavaScript, we usually make a wrapper to keep the original function.</p>\n\n<p>Currying doesn’t call a function. It just transforms it.</p>\n\n<p>Let’s make curry function that performs currying for two-argument functions. In other words, <code>curry(f)</code> for two-argument <code>f(a, b)</code> translates it into <code>f(a)(b)</code></p>\n\n<pre><code>function curry(f) { // curry(f) does the currying transform\n  return function(a) {\n    return function(b) {\n      return f(a, b);\n    };\n  };\n}\n\n// usage\nfunction sum(a, b) {\n  return a + b;\n}\n\nlet carriedSum = curry(sum);\n\nalert( carriedSum(1)(2) ); // 3\n</code></pre>\n\n<p>As you can see, the implementation is a series of wrappers.</p>\n\n<ul>\n<li>The result of <code>curry(func)</code> is a wrapper <code>function(a)</code>.</li>\n<li>When it is called like <code>sum(1)</code>, the argument is saved in the Lexical Environment, and a new wrapper is returned <code>function(b)</code>.</li>\n<li>Then <code>sum(1)(2)</code> finally calls <code>function(b)</code> providing 2, and it passes the call to the original multi-argument sum.</li>\n</ul>\n'}, {'answer_id': 58290342, 'author': 'Prashant Andani', 'author_id': 2545628, 'author_profile': 'https://Stackoverflow.com/users/2545628', 'pm_score': 3, 'selected': False, 'text': '<p>Here is the example of generic and the shortest version for function currying with n no. of params.</p>\n\n<pre><code>const add = a =&gt; b =&gt; b ? add(a + b) : a; \n</code></pre>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const add = a =&gt; b =&gt; b ? add(a + b) : a; \r\nconsole.log(add(1)(2)(3)(4)());</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 60684994, 'author': 'madeinQuant', 'author_id': 5329711, 'author_profile': 'https://Stackoverflow.com/users/5329711', 'pm_score': 0, 'selected': False, 'text': '<p>There is an example of "Currying in ReasonML". </p>\n\n<pre><code>let run = () =&gt; {\n    Js.log("Curryed function: ");\n    let sum = (x, y) =&gt; x + y;\n    Printf.printf("sum(2, 3) : %d\\n", sum(2, 3));\n    let per2 = sum(2);\n    Printf.printf("per2(3) : %d\\n", per2(3));\n  };\n</code></pre>\n'}, {'answer_id': 62166542, 'author': 'sabitha kuppusamy', 'author_id': 7544289, 'author_profile': 'https://Stackoverflow.com/users/7544289', 'pm_score': 3, 'selected': False, 'text': '<p>Currying is one of the higher-order functions of Java Script.</p>\n\n<p>Currying is a function of many arguments which is rewritten such that it takes the first argument and return a function which in turns uses the remaining arguments and returns the value.</p>\n\n<p>Confused?</p>\n\n<p>Let see an example,</p>\n\n\n\n<pre class="lang-js prettyprint-override"><code>function add(a,b)\n    {\n        return a+b;\n    }\nadd(5,6);\n</code></pre>\n\n<p>This is similar to the following currying function,</p>\n\n<pre class="lang-js prettyprint-override"><code>function add(a)\n    {\n        return function(b){\n            return a+b;\n        }\n    }\nvar curryAdd = add(5);\ncurryAdd(6);\n</code></pre>\n\n<p>So what does this code means?</p>\n\n<p>Now read the definition again,</p>\n\n<p>Currying is a function of many arguments which is rewritten such that it takes first argument and return a function which in turns uses the remaining arguments and returns the value.</p>\n\n<p>Still, Confused?\nLet me explain in deep!</p>\n\n<p>When you call this function,</p>\n\n<pre class="lang-js prettyprint-override"><code>var curryAdd = add(5);\n</code></pre>\n\n<p>It will return you a function like this,</p>\n\n<pre class="lang-js prettyprint-override"><code>curryAdd=function(y){return 5+y;}\n</code></pre>\n\n<p>So, this is called higher-order functions. Meaning, Invoking one function in turns returns another function is an exact definition for higher-order function. This is the greatest advantage for the legend, Java Script.\nSo come back to the currying,</p>\n\n<p>This line will pass the second argument to the curryAdd function.</p>\n\n<pre class="lang-js prettyprint-override"><code>curryAdd(6);\n</code></pre>\n\n<p>which in turns results,</p>\n\n<pre class="lang-js prettyprint-override"><code>curryAdd=function(6){return 5+6;}\n// Which results in 11\n</code></pre>\n\n<p>Hope you understand the usage of currying here.\nSo, Coming to the advantages,</p>\n\n<p>Why Currying?</p>\n\n<p>It makes use of code reusability.\nLess code, Less Error.\nYou may ask how it is less code?</p>\n\n<p>I can prove it with ECMA script 6 new feature arrow functions.</p>\n\n<p>Yes! ECMA 6, provide us with the wonderful feature called arrow functions,</p>\n\n<pre class="lang-js prettyprint-override"><code>function add(a)\n    {\n        return function(b){\n            return a+b;\n        }\n    }\n</code></pre>\n\n<p>With the help of the arrow function, we can write the above function as follows,</p>\n\n<pre class="lang-js prettyprint-override"><code>x=&gt;y=&gt;x+y\n</code></pre>\n\n<p>Cool right?</p>\n\n<p>So, Less Code and Fewer bugs!!</p>\n\n<p>With the help of these higher-order function one can easily develop a bug-free code.</p>\n\n<p>I challenge you!</p>\n\n<p>Hope, you understood what is currying. Please feel free to comment over here if you need any clarifications.</p>\n\n<p>Thanks, Have a nice day!</p>\n'}, {'answer_id': 65048818, 'author': 'Abhishek Choudhary', 'author_id': 7946599, 'author_profile': 'https://Stackoverflow.com/users/7946599', 'pm_score': 1, 'selected': False, 'text': '<p>&quot;Currying&quot; is the process of taking the function of multiple arguments and converting it into a series of functions that each take a single argument and return a function of a single argument, or in the case of the final function, return the actual result.</p>\n'}, {'answer_id': 66853947, 'author': 'Yilmaz', 'author_id': 10262805, 'author_profile': 'https://Stackoverflow.com/users/10262805', 'pm_score': 3, 'selected': False, 'text': "<p>Currying means to convert a function of N arity into N functions of arity 1. The <code>arity</code> of the function is the number of arguments it requires.</p>\n<p>Here is the formal definition:</p>\n<pre><code> curry(f) :: (a,b,c) -&gt; f(a) -&gt; f(b)-&gt; f(c)\n</code></pre>\n<p>Here is a real world example that makes sense:</p>\n<p>You go to ATM to get some money. You swipe your card, enter pin number and make your selection and then press enter to submit the &quot;amount&quot; alongside the request.</p>\n<p>here is the normal function for withdrawing money.</p>\n<pre><code>const withdraw=(cardInfo,pinNumber,request){\n    // process it\n       return request.amount\n}\n</code></pre>\n<p>In this implementation function expects us entering all arguments at once. We were going to swipe the card, enter the pin and make the request, then function would run. If any of those steps had issue, you would find out after you enter all the arguments. With curried function, we would create higher arity, pure and simple functions. Pure functions will help us easily debug our code.</p>\n<p>this is Atm with curried function:</p>\n<pre><code>const withdraw=(cardInfo)=&gt;(pinNumber)=&gt;(request)=&gt;request.amount\n</code></pre>\n<p>ATM, takes the card as input and returns a function that expects pinNumber and this function returns a function that accepts the request object and after the successful process, you get the amount that you requested. Each step, if you had an error, you will easily predict what went wrong. Let's say you enter the card and got error, you know that it is either related to the card or machine but not the pin number. Or if you entered the pin and if it does not get accepted you know that you entered the pin number wrong. You will easily debug the error.</p>\n<p>Also, each function here is reusable, so you can use the same functions in different parts of your project.</p>\n"}, {'answer_id': 67447820, 'author': 'WasitShafi', 'author_id': 10249156, 'author_profile': 'https://Stackoverflow.com/users/10249156', 'pm_score': 0, 'selected': False, 'text': "<p>Below is one of currying example in JavaScript, here the <strong>multiply</strong> return the function which is used to multiply <strong>x</strong> by two.</p>\n<pre><code>const multiply = (presetConstant) =&gt; {\n  return (x) =&gt; {\n    return presetConstant * x;\n  };\n};\n\nconst multiplyByTwo = multiply(2);\n\n// now multiplyByTwo is like below function &amp; due to closure property in JavaScript it will always be able to access 'presetConstant' value\n// const multiplyByTwo = (x) =&gt; {\n//   return presetConstant * x;\n// };\n\nconsole.log(`multiplyByTwo(8) : ${multiplyByTwo(8)}`);\n</code></pre>\n<p><strong>Output</strong></p>\n<p><em>multiplyByTwo(8) : 16</em></p>\n"}, {'answer_id': 69050333, 'author': 'Mark Reed', 'author_id': 797049, 'author_profile': 'https://Stackoverflow.com/users/797049', 'pm_score': 1, 'selected': False, 'text': '<p>The other answers have said what currying is: passing fewer arguments to a curried function than it expects is not an error, but instead returns a function that expects the rest of the arguments and returns the same result as if you had passed them all in at once.</p>\n<p>I’ll try to motivate why it’s useful. It’s one of those tools that you never realized you needed until you do. Currying is above all a way to make your programs more expressive - you can combine operations together with less code.</p>\n<p>For example, if you have a curried function <code>add</code>, you can write the equivalent of JS <code>x =&gt; k + x</code>  (or Python <code> lambda x: k + x</code> or Ruby <code>{ |x| k + x }</code> or Lisp <code>(lambda (x) (+ k x))</code> or …)  as just <code>add(k)</code>. In Haskelll you can even use the operator: <code>(k +)</code> or <code>(+ k)</code> (The two forms let you curry either way for non-commutative operators: <code>(/ 9)</code> is a function that divides a number by 9, which is probably the more common use case, but you also have <code>(9 /)</code> for a function that divides 9 by its argument.) Besides being shorter, the curried version contains no made-up parameter name like the <code>x</code> found in all the other versions. It’s not needed. You’re defining a function that adds some constant k to a number, and you don’t need to give that number a name just to talk about the function. Or even to define it. This is an example of what’s called “point-free style”. You can combine operations together given nothing but the operations themselves. You don’t have to declare anonymous functions that do nothing but apply some operation to their argument, because *that’s what the operations already are.</p>\n<p>This becomes very handy with higher-order functions when they’re defined in a currying-friendly way. For instance, a curried <code>map(fn, list)</code> let’s you define a mapper with just <code>map(fn)</code> that can be applied it to any list later.  But currying a map defined instead as <code>map(list, fn)</code> just lets you define a function that will apply some other function to a constant list, which is probably less generally useful.</p>\n<p>Currying reduces the need for things like pipes and threading. In Clojure, you might define a temperature conversion function using the threading macro <code>-&gt;</code>: <code>(defn f2c (deg) (-&gt; deg (- 32) (* 5) (/ 9))</code>. That’s cool, it reads nicely left to right (“subtract 32, multiply by 5 and divide by 9.”) and you only have to mention the parameter twice instead of once for every suboperation… but it only works because <code>-&gt;</code> is a macro that transforms the whole form syntactically before anything is evaluated. It turns into a regular nested expression behind the scenes: <code>(/ (* (- deg 32) 5) 9)</code>.  If the math ops were curried, you wouldn’t need a macro to combine them so nicely, as in Haskell <code>let f2c = (subtract 32) &amp; (* 5) &amp;  (/ 9)</code>. (Although it would admittedly be more idiomatic to use function composition, which reads right to left: <code>(/ 9) . (* 5) . (subtract 32)</code>.)</p>\n<p>Again, it’s hard to find good demo examples; currying is most useful in complex cases where it really helps the readability of the solution, but those take so much explanation just to get you to understand the problem that the overall lesson about currying can get lost in the noise.</p>\n'}, {'answer_id': 73095591, 'author': 'ggorlen', 'author_id': 6243352, 'author_profile': 'https://Stackoverflow.com/users/6243352', 'pm_score': 1, 'selected': False, 'text': '<p>Most of the examples in this thread are contrived (adding numbers). These are useful for illustrating the concept, but don\'t motivate when you might actually use currying in an app.</p>\n<p>Here\'s a practical example from React, the JavaScript user interface library. Currying here illustrates the closure property.</p>\n<p>As is typical in most user interface libraries, when the user clicks a button, a function is called to handle the event. The handler typically modifies the application\'s state and triggers the interface to re-render.</p>\n<p>Lists of items are common user interface components. Each item might have an identifier associated with it (usually related to a database record). When the user clicks a button to, for example, &quot;like&quot; an item in the list, the handler needs to know which button was clicked.</p>\n<p>Currying is one approach for achieving the binding between id and handler. In the code below, <code>makeClickHandler</code> is a function that accepts an id and returns a handler function that has the id in its scope.</p>\n<p>The inner function\'s workings aren\'t important for this discussion. But if you\'re curious, it searches through the array of items to find an item by id and increments its &quot;likes&quot;, triggering another render by setting the state. State is immutable in React so it takes a bit more work to modify the one value than you might expect.</p>\n<p>You can think of invoking the curried function as &quot;stripping&quot; off the outer function to expose an inner function ready to be called. That new inner function is the actual handler passed to React\'s <code>onClick</code>. The outer function is there for the loop body to specify the id that will be in scope of a particular inner handler function.</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const List = () =&gt; {\n  const [items, setItems] = React.useState([\n    {name: "foo", likes: 0},\n    {name: "bar", likes: 0},\n    {name: "baz", likes: 0},\n  ].map(e =&gt; ({...e, id: crypto.randomUUID()})));\n\n  //    .----------.   outer func  inner func\n  //    | currying |         |       |\n  //    `----------`         V       V\n  const makeClickHandler = (id) =&gt; (event) =&gt; {\n    setItems(prev =&gt; {\n      const i = prev.findIndex(e =&gt; e.id === id);\n      const cpy = {...prev[i]};\n      cpy.likes++;\n      return [\n        ...prev.slice(0, i),\n        cpy,\n        ...prev.slice(i + 1)\n      ];\n    });\n  };\n\n  return (\n    &lt;ul&gt;\n    {items.map(({name, likes, id}) =&gt;\n      &lt;li key={id}&gt;\n        &lt;button\n          onClick={\n            /* strip off first function layer to get a click\n               handler  bound to `id` and pass it to onClick */\n            makeClickHandler(id)\n          }\n        &gt;\n          {name} ({likes} likes)\n        &lt;/button&gt;\n      &lt;/li&gt;\n    )}\n    &lt;/ul&gt;\n  );\n};\n\nReactDOM.render(\n  &lt;List /&gt;,\n  document.querySelector("#root")\n);</code></pre>\r\n<pre class="snippet-code-css lang-css prettyprint-override"><code>button {\n  font-family: monospace;\n  font-size: 2em;\n}</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"&gt;&lt;/script&gt;\n&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"&gt;&lt;/script&gt;\n&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"&gt;&lt;/script&gt;\n&lt;div id="root"&gt;&lt;/div&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/36314', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3786/']