Created
May 7, 2012 23:31
-
-
Save LeZuse/2631422 to your computer and use it in GitHub Desktop.
JS Function.prototype.curry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is not currying, that is partial application.