Module: fn

Function composition utilities

Source:

Methods

(static) compose(…fns) → {function}

Creates a right-to-left function composition. The rightmost function can accept multiple arguments; the remaining functions must be unary.

Parameters:
Name Type Attributes Description
fns function <repeatable>

Functions to compose (executed right to left)

Source:
Returns:

A function that passes the result through each function from right to left

Type
function
Example
const addOne = x => x + 1;
const double = x => x * 2;
const doubleThenAddOne = compose(addOne, double);
doubleThenAddOne(3) // => 7 (3 * 2 = 6, 6 + 1 = 7)

(static) pipe(a, …fns) → {function}

Creates a left-to-right function composition pipeline. The first function can accept multiple arguments; the remaining functions must be unary.

Parameters:
Name Type Attributes Description
a function

The first function in the pipeline (can accept multiple arguments)

fns function <repeatable>

The remaining functions to pipe through (each takes one argument)

Source:
Returns:

A function that passes the result through each function from left to right

Type
function
Example
const addOne = x => x + 1;
const double = x => x * 2;
const addOneThenDouble = pipe(addOne, double);
addOneThenDouble(3) // => 8 (3 + 1 = 4, 4 * 2 = 8)