Guest User

Untitled

a guest
Mar 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. // First we need to know the number of arguments of the function.
  2. // Thankfully, functions have a length parameter that tells us just that.
  3. const curry = (fn, length = fn.length) => (...args) => {
  4. if (args.length >= length) {
  5. return fn(...args);
  6. }
  7.  
  8. return curry((...nextArgs) => fn(...args, ...nextArgs), fn.length - args.length);
  9. }
  10.  
  11. const add = curry((a, b, c) => a + b + c);
  12. add(1)(2)(3); // 6
  13. add(1, 2, 3); // 6
  14. add(1, 2)(3); // 6
  15. add(1)(2, 3); // 6
Add Comment
Please, Sign In to add comment