Fix HOF answer

This commit is contained in:
Yangshun Tay 2018-02-13 19:46:42 -08:00
parent 3258164aab
commit 2bba816823
1 changed files with 7 additions and 5 deletions

View File

@ -1474,15 +1474,17 @@ TODO
### What is the definition of a higher-order function?
A higher-order function is any function that takes another function as a parameter, which it uses to operate on some data, or returns a function as a result. Higher-order functions are meant to abstract some operation that is performed repeatedly. The classic example of this is `map`, which takes an array and a function as arguments. `map` then uses this function to transform each item in the array, returning a new array with the transformed data. Other popular examples in JavaScript are `forEach`, `filter`, and `reduce`. A higher-order function doesn't just need to be manipulating arrays as there are many use cases for returning a function from another function. `Array.prototype.bind` is one such example in JavaScript.
A higher-order function is any function that takes one or more functions as arguments, which it uses to operate on some data, and/or returns a function as a result. Higher-order functions are meant to abstract some operation that is performed repeatedly. The classic example of this is `map`, which takes an array and a function as arguments. `map` then uses this function to transform each item in the array, returning a new array with the transformed data. Other popular examples in JavaScript are `forEach`, `filter`, and `reduce`. A higher-order function doesn't just need to be manipulating arrays as there are many use cases for returning a function from another function. `Array.prototype.bind` is one such example in JavaScript.
**Map**
Let say we have an array of names which we need to transform each element to uppercase string.
Let say we have an array of names which we need to transform each string to uppercase.
`const names = ['irish', 'daisy', 'anna']`;
```js
const names = ['irish', 'daisy', 'anna'];
```
The imperative way will be like:
The imperative way will be as such:
```js
const transformNamesToUppercase = function(names) {
@ -1495,7 +1497,7 @@ const transformNamesToUppercase = function(names) {
transformNamesToUppercase(names); // ['IRISH', 'DAISY', 'ANNA']
```
Use `.map(transformerFn)` makes it simpler, easy to reason about and declarative.
Use `.map(transformerFn)` makes the code shorter and more declarative.
```js
const transformNamesToUppercase = function(names) {