We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
add(1)(2)(3); // 6 add(1)(2, 3); // 6 add(1, 2)(3); // 6 add(1, 2, 3); // 6
const curry = fn => { const len = fn.length; return function curried(...args) { if (args.length === len) { return fn.apply(null, args); } return (..._args) => { return curried.apply(null, [...args, ..._args]); }; }; }; const sum = (x, y, z) => x + y + z; const add = curry(sum); // 6 add(1, 2, 3); // 6 add(1,2)(3); // 6 add(1)(2,3); // 6 add(1)(2)(3);
这题里面有 add(1) // 1 ,也就是无论是几个参数都会进行计算。但是看了下所有网友给的题解,都是在函数中来接收参数,然后使用toString 方法来计算总和,那怎么才能去拿到函数的toString计算的值呢?使用 add(1) 返回的是[Function add]这种格式啊
toString
add(1)
[Function add]
The text was updated successfully, but these errors were encountered:
换个更简单一点的
function add(...args) { if (args.length === 3) { return Array.from(args).reduce((pre, curr) => { return pre + curr }, 0) } else { return function curry(...argus) { /* 1. 这里的 apply 很重要,把参数转换成数组,如果只是 add() 调用,上面的 args.length 就一直是 1,这个还没搞明白是为啥 2. 这里很重要的一点,就是将参数聚合起来 */ return add.apply(null, [...argus, ...args]) } } }
Sorry, something went wrong.
No branches or pull requests
解法
参考
The text was updated successfully, but these errors were encountered: