admin 管理员组文章数量: 1086871
柯里化~~
f(a,b,c) → f(a)(b)(c) // 部分函数调用 f(a,b,c) → f(a)(b,c) / f(a,b)(c)
// 没有保存调用时的this
function currying(fn, ...args) {if (args.length >= fn.length) {return fn(...args);} else {return (...args2) => currying(fn, ...args, ...args2);}}
// 保存下this
function curry(fn) {const ctx = this;function inner(...args) {if (args.length === fn.length) return fn.call(ctx, ...args);return (...innerArgs) => inner.call(ctx, ...args, ...innerArgs);}return inner;
}// test
function test(a, b, c) {console.log(a, b, c);
}
const f1 = curry(test)(1);
const f2 = f1(2);
f2(3);// test
function test(a, b, c) {console.log(a, b, c);
}const f1 = curry(test)(1,2);
// const f2 = f1(2);
f1(3);
本文标签: 柯里化
版权声明:本文标题:柯里化~~ 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1687674061a127608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论