liveBook · Manning

Higher-order function

As explained in Grokking Simplicity

A function that accepts functions as arguments, returns functions as results, or otherwise treats functions as values.

A higher-order function accepts another function as an argument, returns a function, or both. It treats behavior as a value, allowing reusable code to abstract over what changes. section 10.10 section 10.15

The practical payoff is replacing repeated low-level structure with a reusable context. Instead of rewriting each array loop, error-handling block, or copy-on-write routine, pass the varying operation as a callback. section 10.13 section 14.21

Common uses include:

  • Iterate over arrays with forEach(), map(), filter(), and reduce(), passing the operation or selection rule as a function. section 12.9 section 12.6
  • Create reusable execution contexts, such as running a callback inside try/catch for consistent error logging. section 10.13
  • Transform one function into another with added behavior, as wrapLogging() does while preserving the original operation. section 11.4
  • Apply operations to object values, including nested data, while centralizing copy-on-write behavior. section 14.3 section 14.21

Replacing a repeated loop

Start with a fixed loop, then expose its changing behavior: function operateOnArray(array, f) { for (var i = 0; i < array.length; i++) { f(array[i]); } } Calling operateOnArray(foods, cookAndEat) separates the reusable iteration context from the operation. Naming the general function forEach() makes the pattern easier to reuse. section 10.10

Returning a function

A higher-order function can manufacture specialized functions: function makeAdder(n) { return function(x) { return n + x; }; } var plus10 = makeAdder(10); plus10(12) produces 22. The returned function behaves like an ordinary function after receiving its captured configuration. section 11.4

A useful refactoring path is to identify the repeated before-and-after structure, extract it into a function, and replace the differing body with a callback. Higher-order functions reduce duplication, but excessive abstraction can cost readability. section 10.1 section 11.6

How Grokking Simplicity covers it

How deep it goes
Defines callbacks, derives functions through refactoring, and demonstrates returned functions, while noting readability tradeoffs without exploring language internals.
Examples
Uses forEach(), map(), filter(), reduce(), withLogging(), wrapLogging(), makeAdder(), update(), and array-loop refactorings.
What it assumes
Assumes basic functions and arrays, while teaching first-class values and the distinction between passing a function and calling it.
What you can do after
Apply it via JavaScript by extracting callbacks, building reusable contexts, composing array operations, and returning specialized functions.
How it connects
Connects first-class functions, callbacks, functional iteration, immutability, copy-on-write, error handling, and functional programming.

Also explained in

Compare how 14 books explain higher-order function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Simplicity. The section links point to the parts of the book it draws on.