liveBook · Manning

Composition

As explained in Functional Programming in JavaScript

A sequence of function invocations linked by a shared intermediate value or by staged transformation.

Function composition builds a larger operation by connecting small functions so that one function’s result becomes the next function’s input. Instead of coordinating loops, temporary variables, and conditionals in one place, split work into focused steps and join them into a single reusable function. section 1.3 section 4.5

A pipeline works only when adjacent functions agree: the value returned by one must be a value the next can accept. In JavaScript, documenting expected inputs and outputs helps make those connections clear, even though the language is loosely typed. A receiving function also needs a parameter to receive the prior result. section 4.2 section 4.1

Clean and validate an input

Start with small functions: trim removes surrounding spaces, normalize removes hyphens, and a length check returns a Boolean. Compose them into cleanInput, then compose that result with the check: R.compose(checkLengthSsn, cleanInput). Calling the resulting function with ' 444-44-4444 ' produces true. Composition runs right to left; R.pipe can express the same stages in left-to-right reading order. section 4.5 section 4.2

Use composition when you need to:

  • Turn independent transformations into one named operation, such as counting words by composing split and length functions. section 4.5
  • Combine functions from your own code or different libraries, rather than being limited to methods on one chained wrapper object. section 4.1
  • Build larger programs from pure, side effect-free components that can themselves be composed again. section 4.5

Method chains and function pipelines both show data moving through sequential operations, but they differ in coupling. A Lodash chain is tied to its wrapper object and available methods. A function pipeline passes ordinary values between standalone functions, so each stage can come from any library or from application code. section 4.1 section 4.0

Build a student display flow

A student-display function can combine trimming and normalizing an ID, finding the student, formatting the student as CSV, and appending the result to a page element. Each step has one job; the composed function exposes the overall job as one call. section 4.5 section 1.3

Figure 4.4. A Function pipeline that starts with a function f and input of type A and generates an object of type B, subsequently passed into g, which outputs an object of type C as the final result. Functions f and g can belong to any library or can be your own functions.

Follow the value as it moves from a function producing type A to B into a function producing B to C; matching B is what makes the pipeline connect. section 4.1

How Functional Programming in JavaScript covers it

How deep it goes
Explains compatibility, manual pipelines, compose implementation, and Ramda composition, while deferring static type systems.
Examples
Uses SSN cleanup and validation, word counting, student display, grade ranking, and Unix-style data processing.
What it assumes
Assumes basic JavaScript functions and introduces pipelines through inputs, outputs, and familiar transformations.
What you can do after
Build it from scratch by composing compatible JavaScript functions with compose or Ramda's R.compose and R.pipe.
How it connects
Connects purity, referential transparency, currying, partial application, function pipelines, and method chains.

Also explained in

Compare how 11 books explain composition →

Related concepts

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