Open NsNe opened 3 years ago
假设有函数
[f1, f2, f3, f4]
function compose(...funcs) { if(funcs.length === 0) { return args => args; } return funcs.reduce((acc, current) => { return (...args) => acc(current(...args)); }); }
function compose(...funcs) { if(funcs.length === 0) { return args => args; } return funcs.reduce((acc, current) => { return (...args) => current(acc(...args)); }); }
function add(a, b = 1) { return a + b; } function square(a) { return a*a; } function plusOne(a) { return a + 1; } function compose(...funcs) { if(funcs.length === 0) { return args => args; } return funcs.reduce((acc, current) => { return (...args) => acc(current(...args)); }); } var addSquareAndPlusOne = compose(add, square, plusOne); addSquareAndPlusOne(1, 2);
假设有函数
f1(f2(f3(f4(x))))
f4(f3(f2(f1(x))))
经典题型测试