Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
fix: sample code in intro to currying
Browse files Browse the repository at this point in the history
Corrected the code sample that didnt work
Amended uncurried example to better tie in with rest of code making the lesson more cohesive
  • Loading branch information
johnkennedy9147 authored and raisedadead committed Jun 21, 2018
1 parent 6de827c commit d4cf9de
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>&nbsp;&nbsp;return x + y + z;<br>}<br><br>//Curried function<br>function curried(x) {<br>&nbsp;&nbsp;return function(y) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return x + y;<br>&nbsp;&nbsp;}<br>}<br>curried(1)(2) // Returns 3</blockquote>",
"<blockquote>//Un-curried function<br>function unCurried(x, y) {<br>&nbsp;&nbsp;return x + y;<br>}<br><br>//Curried function<br>function curried(x) {<br>&nbsp;&nbsp;return function(y) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return x + y;<br>&nbsp;&nbsp;}<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>&nbsp;&nbsp;return x + y + z;<br>}<br>var partialFn = impartial.bind(this, 1, 2);<br>partialFn(10); // Returns 13</blockquote>",
Expand Down Expand Up @@ -1698,4 +1698,4 @@
}
}
]
}
}

0 comments on commit d4cf9de

Please sign in to comment.