From 67cc558da653eafa97ab6ba8448051e34a207c9d Mon Sep 17 00:00:00 2001 From: petern Date: Tue, 16 Oct 2018 10:43:02 +1100 Subject: [PATCH] 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. --- ...-side-effects-using-functional-programming.english.md | 6 +++++- ...uction-to-currying-and-partial-application.english.md | 8 ++++++-- ...e-map-method-to-extract-data-from-an-array.english.md | 9 +++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming.english.md index 0b83fbcb70..0fb12bd216 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming.english.md @@ -63,6 +63,10 @@ console.log(fixedValue); // Should print 4
```js -// solution required +var fixedValue = 4 + +function incrementer() { + return fixedValue + 1 +} ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application.english.md index 1959316689..68d679dd7d 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application.english.md @@ -9,7 +9,11 @@ challengeType: 1 The arity of a function is the number of arguments it requires. Currying a function means to convert a function of N arity into N functions of arity 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: -
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

//Curried function
function curried(x) {
  return function(y) {
    return x + y;
  }
}
curried(1)(2) // Returns 3
+
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

//Curried function
function curried(x) {
  return function(y) {
    return x + y;
  }
} +
//Alternative using ES6 +
const curried = x => y => x + y +
+
curried(1)(2) // Returns 3
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 curried function in the example above:
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
Similarly, partial application 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);
```js -// solution required +const add = x => y => z => x + y + z ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array.english.md index 7c11fad8ba..6eaa784733 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array.english.md @@ -165,7 +165,7 @@ for(var i=0; i < watchList.length; i++){ // Add your code above this line -console.log(rating); +console.log(JSON.stringify(rating)); ``` @@ -178,6 +178,11 @@ console.log(rating);
```js -// solution required +var rating = watchList.map(function(movie) { + return { + title: movie["Title"], + rating: movie["imdbRating"] + } +}) ```