Skip to content

Commit

Permalink
Fix HOF answer
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Feb 14, 2018
1 parent 3258164 commit 2bba816
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 2bba816

Please sign in to comment.