fix: sample code in intro to currying
Corrected the code sample that didnt work Amended uncurried example to better tie in with rest of code making the lesson more cohesive
This commit is contained in:
committed by
mrugesh mohapatra
parent
6de827c8b7
commit
d4cf9deee6
@ -1647,9 +1647,9 @@
|
||||
"The <code>arity</code> of a function is the number of arguments it requires. <code>Currying</code> a function means to convert a function of N <code>arity</code> into N functions of <code>arity</code> 1.",
|
||||
"In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.",
|
||||
"Here's an example:",
|
||||
"<blockquote>//Un-curried function<br>function unCurried(x, y, z) {<br> return x + y + z;<br>}<br><br>//Curried function<br>function curried(x) {<br> return function(y) {<br> return x + y;<br> }<br>}<br>curried(1)(2) // Returns 3</blockquote>",
|
||||
"<blockquote>//Un-curried function<br>function unCurried(x, y) {<br> return x + y;<br>}<br><br>//Curried function<br>function curried(x) {<br> return function(y) {<br> return x + y;<br> }<br>}<br>curried(1)(2) // Returns 3</blockquote>",
|
||||
"This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the <code>curried</code> function in the example above:",
|
||||
"<blockquote>// Call a curried function in parts:<br>var funcForY = curried(1);<br>var funcForZ = funcForY(2);<br>console.log(funcForZ(3)); // Prints 6</blockquote>",
|
||||
"<blockquote>// Call a curried function in parts:<br>var funcForY = curried(1);<br>console.log(funcForY(2)); // Prints 3</blockquote>",
|
||||
"Similarly, <code>partial application</code> can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments.",
|
||||
"Here's an example:",
|
||||
"<blockquote>//Impartial function<br>function impartial(x, y, z) {<br> return x + y + z;<br>}<br>var partialFn = impartial.bind(this, 1, 2);<br>partialFn(10); // Returns 13</blockquote>",
|
||||
@ -1698,4 +1698,4 @@
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user