Skip to content

Instantly share code, notes, and snippets.

@vv13
Created November 28, 2021 05:38
Show Gist options
  • Save vv13/e1a18dd7e77875a62cba8619fc154a25 to your computer and use it in GitHub Desktop.
Save vv13/e1a18dd7e77875a62cba8619fc154a25 to your computer and use it in GitHub Desktop.
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