-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现管道函数 #73
Comments
function pipe(...funcs) {
return function (value) {
return funcs.reduce((currentValue, func) => func(currentValue), value);
}
} |
function pipe(...args1){
return function(...args2){
if(args1.length === 0) return;
if(args1.length === 1) return args1[0](...args2);
return args1.reduce((a, b) => function(){
return b(a(...args2))
})()
}
}; |
function pipe(...fns) {
return function(...args) {
if (fns.length === 0) return;
if (fns.length === 1) return fns[0](...args);
return fns.reduce((acc, fn) => {
if (typeof acc === 'function') {
acc = acc(...args);
args = []; // 清空参数列表,因为之后的函数应该接收上一个函数的结果作为输入
}
return fn(acc); // 将上一步骤得到结果作为当前操作器输入
});
};
} |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: