QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Curry Function

Transforms a function into its curried form.

  • curry
  • function

Install

npx shadcn@latest add https://quicksnip.dev/r/curry.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

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