-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompose.ts
42 lines (38 loc) · 1.25 KB
/
compose.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* > from: https://github.com/reduxjs/redux/blob/master/src/compose.ts
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for the
* resulting composite function.
*
* @param fns The functions to compose.
* @returns A function obtained by composing the argument functions from right
* to left. For example, `compose(f, g, h)` is identical to doing
* `(...args) => f(g(h(...args)))`.
*/
const compose = (...fns: Function[]) => {
if (fns.length === 0) return <T>(arg: T) => arg;
if (fns.length === 1) return fns[0];
return fns.reduce(
(a, b) =>
(...args: any) =>
a(b(...args))
);
};
//test
const add10 = (x: number) => x + 10;
const mul10 = (x: number) => x * 10;
const add100 = (x: number) => x + 100;
// (10 + 100) * 10 + 10 = 1110
console.log(compose(add10, mul10, add100)(10)); // 1110
// 再实现一个从左到右执行的
const composeL2R = (...fns: Function[]) => {
if (fns.length === 0) return <T>(arg: T) => arg;
if (fns.length === 1) return fns[0];
return fns.reduceRight(
(a, b) =>
(...args: any) =>
a(b(...args))
);
};
// (10 + 10) * 10 + 100
console.log(composeL2R(add10, mul10, add100)(10)); //