typestar

Function composition in JavaScript

Composes functions left to right so data flows through them in reading order.

const pipe = (...fns) => (input) =>
  fns.reduce((value, fn) => fn(value), input);

const addOne = (n) => n + 1;
const double = (n) => n * 2;
const addThenDouble = pipe(addOne, double);

Keywords and builtins used here

The run, in numbers

Lines
6
Characters to type
183
Tokens
62
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 35 seconds.

Type this snippet

Step 3 of 5 in Composing functions, step 7 of 16 in Functions & patterns.

← Previous Next →