Module: util/combinators

Helpful combinators
For the non-pedants, a combinator is basically just a function with no free variables.
For the non-pedants, "no free variables" means that the combinator does not have dependencies
on things outside the function (e.g. it only depends on the function parameters).
A strict definition of combinators requires functions as input parameters, but I loosen that here.
That definition really only serves mathematical modeling of state in pure functional terms

Source:

Methods

(static) constant(k) → {function}

Ignores its parameters, and instead always returns a constant value

Parameters:
Name Type Description
k Object

The constant to return

Source:
Returns:
  • A function that will always return the constant
Type
function

(static) identity(i) → {Object}

Returns what was passed in unchanged, occasionally useful as the default transformation function
to avoid special case logic

Parameters:
Name Type Description
i Object

The input

Source:
Returns:

Exactly what was passed in

Type
Object

(static) noop()

A function that does nothing, occasionally useful to avoid special case logic

Source:

(inner) compose(…args) → {function}

Composes two or more functions

Parameters:
Name Type Attributes Description
args function <repeatable>

The functions to compose

Source:
Returns:

A single function that represents the composition of the functions provided

Type
function
Example
function increment (i) { return i + 1; }
function double (i) { return i * 2; }
function triple (i) { return i * 3; }
combinators.compose(increment, double, triple)(1); // returns 7

(inner) curry(fn, …args) → {function}

Curries a function parameters, which is to say that it returns a function with reduced arity.

Parameters:
Name Type Attributes Description
fn function

The function to curry

args * <repeatable>

The arguments to curry

Source:
Returns:
Type
function
Example
function sum (x, y) { return x + y; }
curry(sum, 1)(2); // returns 3
curry(sum, 1, 2)(); // returns 3