Aller au contenu principal

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!'