Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Created May 7, 2012 23:31
Show Gist options
  • Select an option

  • Save LeZuse/2631422 to your computer and use it in GitHub Desktop.

Select an option

Save LeZuse/2631422 to your computer and use it in GitHub Desktop.
JS Function.prototype.curry
function toArray(enum) {
return Array.prototype.slice.call(enum);
}
Function.prototype.curry = function() {
if (arguments.length<1) {
return this; //nothing to curry with - return function
}
var __method = this;
var args = toArray(arguments);
return function() {
return __method.apply(this, args.concat(toArray(arguments)));
}
}
var converter = function(ratio, symbol, input) {
return [(input*ratio).toFixed(1),symbol].join(" ");
}
var kilosToPounds = converter.curry(2.2,"lbs");
var litersToUKPints = converter.curry(1.75, "imperial pints");
var litersToUSPints = converter.curry(1.98, "US pints");
var milesToKilometers = converter.curry(1.62, "km");
kilosToPounds(4); //8.8 lbs
litersToUKPints(2.4); //4.2 imperial pints
litersToUSPints(2.4); //4.8 US pints
milesToKilometers(34); //55.1 km
@drexkdal

drexkdal commented Oct 4, 2017

Copy link
Copy Markdown

That is not currying, that is partial application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment