liveBook · Manning

Combinator

As explained in Functional Programming in JavaScript

A higher-order function that combines other functions to coordinate control flow or transformation in a functional program.

A combinator is a higher-order function that combines functions to coordinate control flow or transformation. It replaces much of the conditional and procedural flow used in imperative code, while keeping business logic inside separate functions. section 4.6

Common combinators handle distinct kinds of coordination:

  • identity returns its input unchanged, making it useful for supplying values, testing composition, and extracting data. section 4.6
  • tap performs an action such as logging, then returns the original value so the action can sit inside a composition. section 4.6
  • alt tries one function and invokes the second only when the first returns a false, null, or undefined value. section 4.6 section 7.2
  • seq applies several functions to the same value, while fork applies two functions and combines their results with a join function. section 4.6

Choosing or creating a student

const showStudent = R.compose( append('#student-info'), csv, alt(findStudent, createNewStudent) ); showStudent('444-44-4444'); Here, alt replaces an if-else branch: it uses findStudent when possible and calls createNewStudent only when needed. section 4.6 section 7.2

Combining two calculations

const computeAverageGrade = R.compose(getLetterGrade, fork(R.divide, R.sum, R.length)); computeAverageGrade([99, 80, 89]); //-> 'B' fork sends the same list to R.sum and R.length, then joins those results with R.divide before converting the average to a grade. section 4.6

const debug = R.tap(debugLog);
const cleanInput = R.compose(normalize, debug, trim);
const isValidSsn = R.compose(debug, checkLengthSsn, debug, cleanInput);

Trace tap inside a composition to see intermediate values without changing the final result. section 4.6

How Functional Programming in JavaScript covers it

How deep it goes
Defines identity, tap, alternation, sequence, and fork with JavaScript implementations and worked compositions.
Examples
Uses student lookup, logging during validation, average-grade calculation, equality comparison, and Lodash chained transformations.
What it assumes
Assumes JavaScript functions and composition, while introducing combinator terminology through familiar imperative alternatives and small examples.
What you can do after
Apply combinators via Ramda and Lodash to compose transformations, choose alternatives, sequence actions, and combine results.
How it connects
Connects higher-order functions and function composition with conditional control flow, debugging, lazy evaluation, and shortcut fusion.

Also explained in

Compare how 4 books explain combinator →

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.