liveBook · Manning

Lambda

As explained in The Art of Code

A lambda is an anonymous function value that can be passed, stored, returned, or invoked as data.

An anonymous function, commonly called a lambda, is a function value without a declared name. It can be passed, stored, returned, or invoked as data. In Java, a lambda implements a functional interface: an interface with one abstract method. section 6.2 section 6.2.1

From boilerplate to behavior

A named class can express the rule that a string must exceed three characters, but it adds ceremony around one method. A lambda keeps the behavior: `StringPredicate myTestingFunction = x -> x.length() > 3;`. The arrow separates parameters from the body, while the target functional-interface type supplies the expected signature. section 6.2 section 6.2.1

Use lambdas as compact behavior, but choose explicitness when compression increases cognitive load:

  • Pass them to functions, store several alternatives in a collection, or return them from a method. section 6.2.1
  • Prefer a lambda over a method reference when explicit parameters make the operation easier to understand. section 5.2
  • Prefer a loop or extracted method when a lambda or pipeline hides too many steps, especially around exception handling. section 5.2 section 6.5
StringPredicate myTestingFunction = x -> x.length() > 3;
boolean isValid = myTestingFunction.test("hello");

Compare the compact Java lambda with equivalent forms in Kotlin, C#, Python, and Haskell through the surrounding examples. section 6.2.1

How The Art of Code covers it

How deep it goes
Defines syntax, target typing, cross-language forms, and readability tradeoffs, but stops short of implementation internals.
Examples
Uses string-length predicates, returned functions, function collections, method references, loops, and Kotlin transformation chains.
What it assumes
Assumes basic functions and types, then teaches functional interfaces and lambda syntax through a predicate example.
What you can do after
Apply it via functional interfaces to pass, store, return, and invoke concise behavior while judging when explicit code reads better.
How it connects
Connects anonymous functions with functional interfaces, first-class functions, pure functions, functional programming, and pipelines.

Also explained in

Compare how 18 books explain lambda →

Related concepts

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