liveBook · Manning

Composition

As explained in The Art of Code

Function composition is the construction of a function by chaining functions so that one function's output becomes the next function's input.

Function composition chains smaller functions so that one function’s output becomes the next function’s input. It lets you build larger behavior from focused transformations, which can also be used independently. section 6.2.2

Composing field sanitization

Instead of one function that both normalizes repeated whitespace and removes special characters, define a function for each transformation, then combine them with andThen. section 6.2.2

Function<String, String> sanitizeField =
 normalizeSpaces.andThen(removeSpecialCharacters);

See the Java composition of normalizeSpaces and removeSpecialCharacters. section 6.2.2

Composition can make transformations simpler, reduce duplication, and make the flow read like a story. Language support varies: some languages provide composition directly, while others use nested calls or extension methods. section 6.2.2

How The Art of Code covers it

How deep it goes
Defines chaining, demonstrates decomposition and Java andThen, and briefly contrasts language support without detailing implementation internals.
Examples
Uses field sanitization split into whitespace normalization and special-character removal, then composed with Java andThen.
What it assumes
Builds on first-class functions and explains how smaller transformations combine into larger behavior.
What you can do after
Build it from scratch by splitting field sanitization into reusable transformations and composing them with Java’s andThen.
How it connects
Connects function composition with first-class functions, functional programming, purity, and data pipelines.

Also explained in

Compare how 12 books explain composition →

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.