Curry Function
Transforms a function into its curried form.
- curry
- function
Install
npx shadcn@latest add https://quicksnip.dev/r/curry.jsonThis 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