Created
November 5, 2013 07:45
-
-
Save shidhincr/7315316 to your computer and use it in GitHub Desktop.
JavaScript Implementation of function currying - By adding a curry method to the Function prototype
This file contains 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.prototype.curry = function( ) { | |
var slice = Array.prototype.slice, | |
self = this, | |
totalargs = self.length, | |
partial = function( args, fn ) { | |
return function( ) { | |
return fn.apply( {}, args.concat( slice.call( arguments ) ) ); | |
} | |
}, | |
fn = function( ) { | |
var args = slice.call( arguments ); | |
return ( args.length < totalargs ) ? | |
partial( args, fn ) : | |
self.apply( {}, slice.apply( arguments, [ 0, totalargs ] ) ); | |
}; | |
return fn; | |
}; |
the arguments is a array-like object, there is no slice in it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Nice implementation. I want to understand why have you used slice.call( arguments ) instead of arguments.slice(). Is it because arguments does not have slice but the implementation of slice is such that it works with the arguments object. One more question, How is the value of arguments decided? In nested functions, will the arguments have the arguments of it immediate wrapping function? Thank you