Skip to content

Instantly share code, notes, and snippets.

@vv13
Created Nov 28, 2021
Embed
What would you like to do?
JavaScript 函数柯里化
function curry(fn) {
return function curried(...args) {
if (fn.length === args.length) {
return fn.apply(this, args);
}
return (...extraArgs) => {
return curried.apply(this, args.concat(extraArgs));
};
};
}
@vv13
Copy link
Author

vv13 commented Nov 30, 2021

Assume we have a function, receiving several parameter and do something:

function doSomething(task1, task2, task3) {
  task1()
  task2()
  task3()
}

Then we should implement a fn to make function Curry:

function curry() {}

const curried = curry(doSomething)

curried(task1, task2, task3)
curried(task1, task2)(task3)
curried(task1)(task2)(task3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment