Skip to main content

compose()

const compose: {(): <T>(x) => T; <A, B>(f): (a) => B; <A, B, C>(f, g): (a) => C; <A, B, C, D>(f, g, h): (a) => D; <A, B, C, D, E>(f, g, h, i): (a) => E; <A, B, C, D, E, F>(f, g, h, i, j): (a) => F; <A, B, C, D, E, F, G>(f, g, h, i, j, k): (a) => G; } = flowRight

Alias for flowRight.

(): <T>(x) => T

<A, B>(f): (a) => B

<A, B, C>(f, g): (a) => C

<A, B, C, D>(f, g, h): (a) => D

Show 3 more signatures

<A, B, C, D, E>(f, g, h, i): (a) => E

<A, B, C, D, E, F>(f, g, h, i, j): (a) => F

<A, B, C, D, E, F, G>(f, g, h, i, j, k): (a) => G

Composes functions right-to-left, passing the result of each to the previous. This is the opposite of pipe (also known as compose).

ALIAS

compose


Type Parametersโ€‹

A: Aโ€‹

B: Bโ€‹

C: Cโ€‹

D: Dโ€‹

E: Eโ€‹

F: Fโ€‹

G: Gโ€‹


Parametersโ€‹

Overload 1:

f: (a) => Bโ€‹

Overload 2:

f: (b) => Cโ€‹

g: (a) => Bโ€‹

Overload 3:

f: (c) => Dโ€‹

g: (b) => Cโ€‹

h: (a) => Bโ€‹

Overload 4:

f: (d) => Eโ€‹

g: (c) => Dโ€‹

h: (b) => Cโ€‹

i: (a) => Bโ€‹

Show 2 more overloads

Overload 5:

f: (e) => Fโ€‹

g: (d) => Eโ€‹

h: (c) => Dโ€‹

i: (b) => Cโ€‹

j: (a) => Bโ€‹

Overload 6:

f: (e) => Gโ€‹

g: (d) => Fโ€‹

h: (c) => Eโ€‹

i: (b) => Dโ€‹

j: (a) => Cโ€‹

k: (a) => Bโ€‹


Returns: <T>(x): Tโ€‹

The composed function.

Type Parametersโ€‹

T: Tโ€‹

x: T
Returns: T


Sinceโ€‹

2.0.0


Performanceโ€‹

Uses reduce for composition. Overhead is minimal for typical use.


Exampleโ€‹

const add10 = (x: number) => x + 10;
const multiply2 = (x: number) => x * 2;
const square = (x: number) => x * x;

// Right to left: square(multiply2(add10(5)))
const composed = flowRight(square, multiply2, add10);
composed(5); // => 900 (5 + 10 = 15, 15 * 2 = 30, 30 * 30 = 900)

// Compare with pipe (left to right)
// pipe would be: add10(multiply2(square(5)))

// String transformation
const trim = (s: string) => s.trim();
const upper = (s: string) => s.toUpperCase();
const exclaim = (s: string) => s + '!';

const shout = flowRight(exclaim, upper, trim);
shout(' hello '); // => 'HELLO!'