Skip to content

Commit

Permalink
Merge pull request GitbookIO#39 from hasselg/ho_functions
Browse files Browse the repository at this point in the history
Corrected grammar and improved clarity of HOF descriptions
  • Loading branch information
AaronO committed Apr 12, 2014
2 parents 153dfe8 + 6aa0e89 commit 06f9987
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions functions/higher_order.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ Higher order functions are functions that manipulate other functions.
For example, a function can take other functions as arguments and/or produce a function as its return value.
Such *fancy* functional techniques are powerful constructs available to you in JavaScript and other high-level languages like python, lisp, etc.

We will now create two simple functions `add_2` and `double` and higher order
function called `map(func, list)` which applies the function `func` (the first argument)
to each of the elements in the array `list` (the second argument).
We will now create two simple functions, `add_2` and `double`, and a higher order function called `map`. `map` will accept two arguments, `func` and `list` (its declaration will therefore begin `map(func,list)`), and return an array. `func` (the first argument) will be a function that will be applied to each of the elements in the array `list` (the second argument).

```javascript
// Define two simple functions
Expand Down Expand Up @@ -35,11 +33,9 @@ map(add_2, [5,6,7]) // => [7, 8, 9]
map(double, [5,6,7]) // => [10, 12, 14]
```

The functions in the above example were intentionally simple,
and serves to illustrate that passing functions as arguments
to other functions allows for flexibility when building things.
The functions in the above example are simple. However, when passed as arguments to other functions, they can be composed in unforeseen ways to build more complex functions.

For example, if we notice that we use the invocations `map(add_2, ...)` and `map(double, ...)` very often in our code, we could decide we want to create two special-purpse list processors that have the desired operation baked into them. Using function composition, we could do this as follows:
For example, if we notice that we use the invocations `map(add_2, ...)` and `map(double, ...)` very often in our code, we could decide we want to create two special-purpse list processing functions that have the desired operation baked into them. Using function composition, we could do this as follows:

```javascript
process_add_2 = function(list) {
Expand Down Expand Up @@ -76,7 +72,7 @@ process_double([5,6,7]) // => [10, 12, 14]


Let's look at another example.
We'll create a function called `buildMultiplier` that takes a number `x` as input and returns a function that multiplies it's argument by x :
We'll create a function called `buildMultiplier` that takes a number `x` as input and returns a function that multiplies its argument by `x` :

```javascript
var buildMultiplier = function(x) {
Expand Down

0 comments on commit 06f9987

Please sign in to comment.