Skip to content

Instantly share code, notes, and snippets.

@DanyelMorales
Created April 9, 2018 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanyelMorales/18a8b5044073b88ab3a81dacf0363892 to your computer and use it in GitHub Desktop.
Save DanyelMorales/18a8b5044073b88ab3a81dacf0363892 to your computer and use it in GitHub Desktop.
Curry stable method for javascript and Typescript.
// Define new methods, function helper
Function.method = function(name, fx) {
this.prototype[name] = fx;
};
// Define curry method
Function.method("curry", function() {
const slice = Array.prototype.slice;
const args = slice.apply(arguments);
const that = this;
return function() {
return that.apply(null, args.concat(slice.apply(arguments)));
};
});
// Original function
function add(a, b) {
return a + b;
}
// creating our curried function, and invoking
const add1 = add.curry(1);
// returns 46
console.log(add1(45));
// invoking our function with no curry
// returns 3
console.log(add(1, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment