Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 499 Bytes

curry-function.md

File metadata and controls

24 lines (22 loc) · 499 Bytes
title description author tags
Curry Function
Transforms a function into its curried form.
axorax
curry,function
const curry = (func) => {
  const curried = (...args) => {
    if (args.length >= func.length) {
      return func(...args);
    }
    return (...nextArgs) => curried(...args, ...nextArgs);
  };
  return curried;
};

// Usage:
const add = (a, b, c) => a + b + c;
const curriedAdd = curry(add);
curriedAdd(1)(2)(3); // Returns: 6
curriedAdd(1, 2)(3); // Returns: 6