Skip to content

Instantly share code, notes, and snippets.

@ilmpc
Created May 9, 2024 16:08
Show Gist options
  • Select an option

  • Save ilmpc/b1aae4dc6bd50fddedbc47b9121ea250 to your computer and use it in GitHub Desktop.

Select an option

Save ilmpc/b1aae4dc6bd50fddedbc47b9121ea250 to your computer and use it in GitHub Desktop.
const mergeArgs = (savedArgs, args, i = 0) =>
savedArgs.map((e) => {
if (e === curry.placeholder && i < args.length) {
return args[i++];
}
return e;
});
const curry =
(
fn,
vacantArgs = fn.length,
savedArgs = [...new Array(vacantArgs)].map(() => curry.placeholder)
) =>
(...args) => {
args = args.slice(0, vacantArgs);
const filledArgs = args.filter((e) => e !== curry.placeholder).length;
if (filledArgs >= vacantArgs) {
return fn(...mergeArgs(savedArgs, args));
}
return curry(fn, vacantArgs - filledArgs, mergeArgs(savedArgs, args));
};
curry.placeholder = Symbol();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment