Add solutions and update functional programming challenge instructions (#18931)
* feat: Update intro to currying - Add solutions - Add ES6 examples * feat: Add solution to FP mutations * feat: Add solution to function-programming map - Add solutions, both vanilla and ES6 arrow functions - Add JSON.stringify to console.log, makes it easier to see what the resulting output is rather than showing e.g. [object, object, object] * update: used the contributor's ES6 solution Contributor originally had a non-ES6 solution and an ES6 solution. I removed the non-ES6 solution.
This commit is contained in:
		| @@ -63,6 +63,10 @@ console.log(fixedValue); // Should print 4 | |||||||
| <section id='solution'> | <section id='solution'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // solution required | var fixedValue = 4 | ||||||
|  |  | ||||||
|  | function incrementer() { | ||||||
|  |   return fixedValue + 1 | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
| </section> | </section> | ||||||
|   | |||||||
| @@ -9,7 +9,11 @@ challengeType: 1 | |||||||
| 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. | 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. | 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: | Here's an example: | ||||||
| <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> | <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>//Alternative using ES6 | ||||||
|  | <br>const curried = x => y => x + y | ||||||
|  | <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: | 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>console.log(funcForY(2)); // Prints 3</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. | 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. | ||||||
| @@ -65,6 +69,6 @@ add(10)(20)(30); | |||||||
| <section id='solution'> | <section id='solution'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // solution required | const add = x => y => z => x + y + z | ||||||
| ``` | ``` | ||||||
| </section> | </section> | ||||||
|   | |||||||
| @@ -165,7 +165,7 @@ for(var i=0; i < watchList.length; i++){ | |||||||
|  |  | ||||||
| // Add your code above this line | // Add your code above this line | ||||||
|  |  | ||||||
| console.log(rating); | console.log(JSON.stringify(rating)); | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
| @@ -178,6 +178,11 @@ console.log(rating); | |||||||
| <section id='solution'> | <section id='solution'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // solution required | var rating = watchList.map(function(movie) { | ||||||
|  |   return { | ||||||
|  |     title: movie["Title"], | ||||||
|  |     rating: movie["imdbRating"] | ||||||
|  |   } | ||||||
|  | }) | ||||||
| ``` | ``` | ||||||
| </section> | </section> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user