Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // First we need to know the number of arguments of the function.
- // Thankfully, functions have a length parameter that tells us just that.
- const curry = (fn, length = fn.length) => (...args) => {
- if (args.length >= length) {
- return fn(...args);
- }
- return curry((...nextArgs) => fn(...args, ...nextArgs), fn.length - args.length);
- }
- const add = curry((a, b, c) => a + b + c);
- add(1)(2)(3); // 6
- add(1, 2, 3); // 6
- add(1, 2)(3); // 6
- add(1)(2, 3); // 6
Add Comment
Please, Sign In to add comment