liveBook · Manning

Lambda

As explained in Grokking Simplicity

A function expression that has no name and can be defined inline where it is used.

An anonymous function is a function without a name. It is usually defined inline, directly where it is used, rather than assigned to a variable or referenced later. This makes it useful for short, context-specific behavior that runs once. section 10.12 section 12.5

Distinguish the three common ways to define a function:

  • Define and name it globally when many parts of a program may reuse it. section 10.12
  • Define and name it locally when it needs local values but should remain unavailable outside its scope. section 10.12
  • Define it inline as an anonymous function when the behavior is short, contextual, and needed once. section 10.12

Passing behavior to map()

var friendGreetings = map(friendsNames, function(name) { return "Hello, " + name; }); The function receives one array element at a time. Naming the argument name communicates that expectation, while the inline definition keeps this one-use behavior beside map(). section 12.4 section 12.5

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

See an anonymous function passed directly to withLogging(), contrasted with named and locally assigned alternatives. section 10.12

How Grokking Simplicity covers it

How deep it goes
Defines anonymous functions through global, local, and inline contrasts, then demonstrates callbacks; stops short of implementation internals.
Examples
Demonstrates logging callbacks, customer email generation, greetings, array iteration, grouping, and moving-average pipelines.
What it assumes
Assumes basic function syntax and arrays while teaching function arguments, scope, inline definitions, and higher-order callbacks.
What you can do after
Apply it via map() and forEach() to pass one-use behavior directly where iteration or another operation consumes it.
How it connects
Connects anonymous functions to inline functions, higher-order functions, callbacks, map(), forEach(), closures, and Java 8 lambda expressions.

Also explained in

Compare how 18 books explain lambda →

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.