liveBook · Manning

Inline function

As explained in Grokking Simplicity

A function defined at the place in a program where it is used rather than assigned to a separate name.

An inline function is defined where it is used instead of being assigned a separate name. It is usually anonymous and works best for a short operation needed once. section 10.12 section 12.5

Distinguish three ways to define and pass a function:

  • Define it globally and refer to it by name from elsewhere. section 10.12
  • Define and name it locally when it needs local values but should remain within that scope. section 10.12
  • Define it inline as an anonymous function when the operation is short and used once. section 12.5

Passing an inline callback

map(customers, function(customer) { return emailForCustomer(customer, goods, bests); }); map calls the callback once for each array element, so naming its argument customer makes the operation clear. section 12.4

withLogging(function() { 
    saveUserData(user); 
});

Compare an inline callback with a separately named function passed to withLogging. section 10.12

How Grokking Simplicity covers it

How deep it goes
Defines inline, anonymous, global, and local functions, then demonstrates callbacks and refactoring limits.
Examples
Uses customer emails, greetings, logging, and purchase filtering and mapping to demonstrate inline callbacks.
What it assumes
Assumes basic function syntax and teaches the distinction between named functions, anonymous functions, and inline definitions.
What you can do after
Apply inline functions as map callbacks and recognize when naming a function improves reuse and clarity.
How it connects
Connects inline functions to anonymous functions, first-class functions, higher-order functions, callbacks, and map.

Also explained in

Compare how 4 books explain inline 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.