Created
May 31, 2012 13:07
-
-
Save dervalp/2843309 to your computer and use it in GitHub Desktop.
Understanding 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
| var add = function(a, b, c) { | |
| return a + b + c; | |
| }; | |
| Function.prototype.method = function(name, func) { | |
| this.prototype[name] = func; | |
| return this; | |
| }; | |
| Function.method('curry', function() { | |
| //keep the value of curry inside a closure | |
| var slice = Array.prototype.slice, | |
| args = slice.apply(arguments), | |
| that = this; | |
| console.log("arguments in curry is - " + arguments); | |
| console.log("args in curry is - " + args); | |
| return function() { | |
| //at that point the arguments is the arguments of the function (here add) | |
| var finalarg = args.concat(slice.apply(arguments)); | |
| console.log("arguments in func is - " + arguments); | |
| console.log("finalarg in func is - " + finalarg); | |
| return that.apply(null, finalarg); | |
| }; | |
| }); | |
| var add1 = add.curry(1,5); | |
| console.log(add1(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment