liveBook · Manning

composeM

As explained in The Joy of JavaScript

A composition function that sequences operations by using monadic flatMap to pass intermediate results through a chain.

composeM sequences functions that return monadic values. It uses flatMap to pass each intermediate result onward while flattening the wrapped values, so the monad can control the pipeline. section 5.5.6

The distinction from ordinary composition is straightforward:

  • Use compose for functions returning ordinary, unwrapped values; use composeM for functions returning monads. section 5.5.6
  • composeM2 applies one monad-returning function through another, and composeM reduces that binary operation across a longer pipeline. section 5.5.6
  • With Validation, a failed check skips subsequent checks and carries the failure result through the chain. section 5.5.5 section 5.5.6

Block validation

A block can be validated by composing Validation.of with checks for timestamps, index, tampering, difficulty, linkage, and length. The resulting pipeline remains point-free while Validation steers success or failure. section 5.5.5 section 5.5.6

Listing 5.22 Block validation with composeM
class Block {
  //...

  isValid() {
     const {
        index: previousBlockIndex,  
        timestamp: previousBlockTimestamp
      } = this.#blockchain.lookUp(this.previousHash);
     
      return composeM(
         checkTimestamps(previousBlockTimestamp),
         checkIndex(previousBlockIndex),
         checkTampering,
         Validation.of
      )(Object.freeze(this));
  }
}

Inspect a complete composeM validation pipeline applied to an immutable block. section 5.5.6

How The Joy of JavaScript covers it

How deep it goes
Defines composeM2, reduces it across functions, and explains flatMap-based flattening alongside map-based composition.
Examples
Uses blockchain block validation with timestamp, index, tampering, difficulty, linkage, and length checks.
What it assumes
Assumes familiarity with compose, curry, map, and monads while extending point-free function-composition ideas.
What you can do after
Apply it via composeM to chain Validation-returning checks with point-free structure and automatic failure propagation.
How it connects
Connects function composition, point-free coding, monads, map, flatMap, and Validation.

Related concepts

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