-
Notifications
You must be signed in to change notification settings - Fork 108
/
compose.js
65 lines (54 loc) · 1.81 KB
/
compose.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// compose(f1, f2, f3, ....)
function compose() {
const fn = Array.prototype.slice.call(arguments);
for (const f of fn) {
if (typeof f !== 'function') {
throw new TypeError(`${f} must be a function.`)
}
}
return fn.reduce((cur, next) => (...args) => cur(next(...args))), next => next);
}
// 从右到左合成函数
function compose() {
const args = Array.prototype.slice.call(arguments);
const len = args.length;
let index = len;
while(index--) {
if (typeof args[index] !== 'function') {
throw new TypeError(`${args[index]} must be a function in params of ${index}`)
}
}
return function () {
let last = len - 1;
// 如果存在两个及以上参数,则调用最后一个参数函数,并传入内层函数;否则直接返回第 1 个参数函数。
let result = last > 0
? args[last].apply(this, arguments)
: args[0];
while (last--) {
// 把右侧函数的执行结果作为参数传给左侧参数函数,并调用。
result = args[last].call(this, result);
}
return result;
}
}
// 从左到右合成函数
function composeLeft() {
return compose.apply(null, [].reverse.call(arguments));
}
// test
var add = function (x) { return x + 5; }
var mul= function (x) { return x * 5; }
var sub= function (x) { return x - 5; }
var div = function (x) { return x / 5; }
var fn = compose(add, mul, sub, div);
console.log(fn(50)); // 30
var fn = compose(add, compose(mul, sub, div));
console.log(fn(50)); // 30
var fn = compose(compose(add, mul), sub, div);
console.log(fn(50)); // 30
var fn1 = composeLeft(add, mul, sub, div);
console.log(fn1(50)); // 54
var fn1 = composeLeft(add, compose(mul, sub, div));
console.log(fn1(50)); // 30
var fn1 = composeLeft(compose(add, mul), sub, div);
console.log(fn1(50)); // 50