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 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.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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the arguments is a array-like object, there is no slice in it.