Skip to content

Instantly share code, notes, and snippets.

@rostegg
Created July 5, 2021 18:14
Show Gist options
  • Save rostegg/9c0f9e9c303519c40b156d3735e88437 to your computer and use it in GitHub Desktop.
Save rostegg/9c0f9e9c303519c40b156d3735e88437 to your computer and use it in GitHub Desktop.
Curry function with unknown length of params
/*
const toCurryFn = (...args) => {...}
const curried = curry(toCurryFn);
...
*/
function curry(fn) {
const argumentsArray = [];
function subCurry(...args) {
argumentsArray.push(...args);
return subCurry;
}
subCurry[Symbol.toPrimitive] = () => {
const result = fn.apply(this, argumentsArray);
argumentsArray.length = 0;
return result;
};
return subCurry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment