Curry a Function
Curry a function so partial application returns a new function until every argument is supplied, enabling reusable configured variants.
Code
export function curry(fn) {
const arity = fn.length;
return function curried(...args) {
if (args.length >= arity) {
return fn.apply(this, args);
}
return function next(...more) {
return curried.apply(this, args.concat(more));
};
};
}How it works
The working surface is the functions curry, curried and next — everything else is wiring.
What to watch for
- The guard
args.length >= arityshort-circuits the flow with an earlyreturnwhen its condition holds.
Using it
The main signature reads export function curry(fn)
curry is what another module would consume; nothing else here is load-bearing for callers.
Key statements
return curried.apply(this, args.concat(more));
return fn.apply(this, args);
Interactive Tools for This Topic
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. The file defines `curry`, `curried`, `next` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.