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 — Merge pull request #39 from hasselg/ho_functions · seshi/javascript@06f9987

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
+4-8Lines changed: 4 additions & 8 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ 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(func, list)` which applies the function `func` (the first argument)
9-
to each of the elements in the array `list` (the second argument).
7+
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).
108

119
```javascript
1210
// Define two simple functions
@@ -35,11 +33,9 @@ map(add_2, [5,6,7]) // => [7, 8, 9]
3533
map(double, [5,6,7]) // => [10, 12, 14]
3634
```
3735

38-
The functions in the above example were intentionally simple,
39-
and serves to illustrate that passing functions as arguments
40-
to other functions allows for flexibility when building things.
36+
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.
4137

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:
38+
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:
4339

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

7773

7874
Let's look at another example.
79-
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 :
75+
We'll create a function called `buildMultiplier` that takes a number `x` as input and returns a function that multiplies its argument by `x` :
8076

8177
```javascript
8278
var buildMultiplier = function(x) {

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.