Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active December 28, 2023 05:56
Show Gist options
  • Save lovasoa/e9c51b84ead1ae0b2659 to your computer and use it in GitHub Desktop.
Save lovasoa/e9c51b84ead1ae0b2659 to your computer and use it in GitHub Desktop.
Partial-evaluation for javascript.
/** pe
* @argument f: the multiple-argument function to turn into a partially-evaluatable
* @returns : A single-argument function that applies its argument as the first argument of f, and returns the partially-evaluated function
* @exemple: pe((a,b)=>a+b)(9)(1) === 10
*/
function pe(f, context, args) {
if(!args) args = [];
if (args.length === f.length) return f.apply(context, args);
return function partial (a) {
var args_copy = args.concat.apply(args, arguments);
return pe(f, context, args_copy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment