GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve higher_order.md function signatures · atik2002/javascript@dd94640

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • functions
    • higher_order.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎functions/higher_order.md‎

Copy file name to clipboardExpand all lines: functions/higher_order.md
+11-11Lines changed: 11 additions & 11 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ Higher order functions are functions that manipulate other functions.
44
For example, a function can take other functions as arguments and/or produce a function as its return value.
55
Such *fancy* functional techniques are powerful constructs available to you in JavaScript and other high-level languages like python, lisp, etc.
66

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)
99
to each of the elements in the array `list` (the second argument).
1010

1111
```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;
1515
};
1616
var double = function(x) {
1717
return 2 * x;
@@ -20,23 +20,23 @@ var double = function(x) {
2020
// map is cool function that accepts 2 arguments:
2121
// func the function to call
2222
// 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
2525
for(idx in list) {
2626
output.push( func(list[idx]) );
2727
}
2828
return output;
2929
}
3030

3131

32-
// We use map to apply a function to an entire list
32+
// We use map to apply a function to an entire list
3333
// of inputs to "map" them to a list of corresponding outputs
3434
map(add_2, [5,6,7]) // => [7, 8, 9]
3535
map(double, [5,6,7]) // => [10, 12, 14]
3636
```
3737

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
4040
to other functions allows for flexibility when building things.
4141

4242
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`
5656
and returns a `func`-processor, that is, a function that applies `func` to each input in list.
5757

5858
```javascript
59-
// a function that generates a list processor that performs
59+
// a function that generates a list processor that performs
6060
var buildProcessor = function(func) {
6161
var process_func = function(list) {
6262
return map(func, list);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.