@@ -4,14 +4,14 @@ Higher order functions are functions that manipulate other functions. |
4 | 4 | For example, a function can take other functions as arguments and/or produce a function as its return value. |
5 | 5 | Such *fancy* functional techniques are powerful constructs available to you in JavaScript and other high-level languages like python, lisp, etc. |
6 | 6 |
|
7 | | -We will now create two simple functions `add_2` and `double` and higher order |
8 | | -function called `map(f,list)` which applies the function `f` (the first argument) |
| 7 | +We will now create two simple functions `add_2` and `double` and higher order |
| 8 | +function called `map(func, list)` which applies the function `func` (the first argument) |
9 | 9 | to each of the elements in the array `list` (the second argument). |
10 | 10 |
|
11 | 11 | ```javascript |
12 | | -// Define two simple functions |
13 | | -var add_2 = function(x) { |
14 | | - return x + 2; |
| 12 | +// Define two simple functions |
| 13 | +var add_2 = function(x) { |
| 14 | + return x + 2; |
15 | 15 | }; |
16 | 16 | var double = function(x) { |
17 | 17 | return 2 * x; |
@@ -20,23 +20,23 @@ var double = function(x) { |
20 | 20 | // map is cool function that accepts 2 arguments: |
21 | 21 | // func the function to call |
22 | 22 | // list a array of values to call func on |
23 | | -var map = function(func, list) { |
24 | | - var output=[]; // output list |
| 23 | +var map = function(func, list) { |
| 24 | + var output=[]; // output list |
25 | 25 | for(idx in list) { |
26 | 26 | output.push( func(list[idx]) ); |
27 | 27 | } |
28 | 28 | return output; |
29 | 29 | } |
30 | 30 |
|
31 | 31 |
|
32 | | -// We use map to apply a function to an entire list |
| 32 | +// We use map to apply a function to an entire list |
33 | 33 | // of inputs to "map" them to a list of corresponding outputs |
34 | 34 | map(add_2, [5,6,7]) // => [7, 8, 9] |
35 | 35 | map(double, [5,6,7]) // => [10, 12, 14] |
36 | 36 | ``` |
37 | 37 |
|
38 | | -The functions in the above example were intentionally simple, |
39 | | -and serves to illustrate that passing functions as arguments |
| 38 | +The functions in the above example were intentionally simple, |
| 39 | +and serves to illustrate that passing functions as arguments |
40 | 40 | to other functions allows for flexibility when building things. |
41 | 41 |
|
42 | 42 | 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: |
@@ -56,7 +56,7 @@ Now let's create a function called `buildProcessor` that takes a function `func` |
56 | 56 | and returns a `func`-processor, that is, a function that applies `func` to each input in list. |
57 | 57 |
|
58 | 58 | ```javascript |
59 | | -// a function that generates a list processor that performs |
| 59 | +// a function that generates a list processor that performs |
60 | 60 | var buildProcessor = function(func) { |
61 | 61 | var process_func = function(list) { |
62 | 62 | return map(func, list); |
|