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
function currying(fn) { function curried(...args) { // 当已经传入的参数, 大于等于需要的参数时, 就执行函数 if (args.length >= fn.length) { return fn.apply(this, args) } else { // 没有达到个数时, 需要返回一个新的函数, 继续来接受参数 return function(...args2) { // 接受到参数后, 需要递归调用 curried 来检查函数的参数个数时否达到 return curried.apply(this, [...args, ...args2]) } } } return curried } // test function add(num1, num2, num3) { return num1 + num2 + num3 } const curryAdd = currying(add) console.log(curryAdd(10)(20)(30)) console.log(curryAdd(10, 20)(30)) console.log(curryAdd(10, 20, 30))
The text was updated successfully, but these errors were encountered:
function curry(fn, args) { let length = fn.length; args = args || []; return function() { let _args = args.slice(0); let arg; for (let i = 0; i < arguments.length; i++) { arg = arguments[i]; _args.push(arg) } if (_args.length < length) { return curry.call(this, fn, _args); } else { return fn.apply(fn, _args); } } }
Sorry, something went wrong.
@lyx-jay 可以配一下代码高亮~
// 柯里化函数 const curry = (func: Function) => { return function curryFunc(...args: any[]) { // 实参 < 形参, 返回一个函数,并等待接受剩余参数 if (args.length < func.length) { return function () { // 合并每次调用函数时传递的参数 return curryFunc(...[...args, ...Array.from(arguments)]); }; } // 当实参 >= 形参时,直接执行函数返回结果 return func(...args); }; }; function test(n1, n2, n3) { return n1 + n2 + n3; } const result = curry(test); console.log('sum', result(1)(2)(3)); // 6 console.log('sum', result(1, 2)(1)); // 4 console.log('sum', result(1)(2)(1)); // 4 console.log('sum', result(1, 2, 1)); // 4
No branches or pull requests
The text was updated successfully, but these errors were encountered: