Skip to content
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

026.curry #26

Open
mewcoder opened this issue Mar 19, 2024 · 5 comments
Open

026.curry #26

mewcoder opened this issue Mar 19, 2024 · 5 comments

Comments

@mewcoder
Copy link
Owner

mewcoder commented Mar 19, 2024

// 实现
function curry() {}

// 测试
function sum(a, b, c) {
  return a + b + c;
}
const curriedSum = curry(sum);

console.log(curriedSum(1)(2)(3)); // 应该输出 6
console.log(curriedSum(1, 2)(3)); // 应该输出 6
console.log(curriedSum(1)(2, 3)); // 应该输出 6
@mewcoder
Copy link
Owner Author

// 使用递归函数调用
function curry(fn) {
    return function curried(...args) {
        if (args.length >= fn.length) {
            return fn(...args);
        }
        return (...nextArgs) => curried(...args, ...nextArgs);
    };
}

@mewcoder
Copy link
Owner Author

// 使用闭包
function curry(fn) {
    let len = fn.length
    let curArgs = []
    return function curried(...args) {
        curArgs.push(...args)
        if (curArgs.length >= len) {
            const res = fn(...curArgs)
            curArgs = [] // 注意要清空
            return res
        } else {
            return curried
        }
    };
};

@mewcoder
Copy link
Owner Author

mewcoder commented Mar 20, 2024

考虑 this 绑定

function curry(fn) {
    return function curried(...args) {
        if (args.length >= fn.length) {
            return fn.apply(this, args);
        } else {
            return function (...args2) {
                return curried.apply(this, args.concat(args2));
            }
        }
    };
}
// 使用 bind
function curry(fn) {
    return function curried(...args) {
        if (args.length >= fn.length) {
            return fn.apply(this, args);
        }
        return curried.bind(this, ...args);
    };
};

@mewcoder
Copy link
Owner Author

mewcoder commented Mar 20, 2024

不定参数

function sum(...args) {
  return args.reduce((pre, cur) => pre + cur, 0)
}

使用空参数调用触发函数 curriedSum(1)(2)(3)()

// 使用闭包
function curry(fn) {
  let allArgs = []
  return function curried(...args) {
    if (args.length === 0) {
      const res = fn(...allArgs);
      allArgs = []
      return res
    }
    allArgs.push(...args);
    return curried;
  };
};
// 递归
function curry(fn) {
    return function curried(...args) {
        return (...nextArgs) => {
            if (nextArgs.length === 0) {
                return fn(...args);
            }
            return curried(...args, ...nextArgs);
        };
    };
};

@mewcoder
Copy link
Owner Author

mewcoder commented Mar 20, 2024

相关题目:实现 sum 的链式调用

function sum(...args) {
    const arr = [...args];

    function add(...rest) {
        arr.push(...rest);
        return add;
    }

    // 隐式调用,注意直接 console.log 不行
    add.valueOf = add.toString = function () {
        return arr.reduce((pre, cur) => pre + cur, 0)
    };

    return add;
}

console.log(+sum1(1)(2)(3)(10)(10, 20))
console.log('sum:' + sum1(1)(2)(3)(10)(10, 20))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant