Skip to content

Instantly share code, notes, and snippets.

@shidhincr
Created November 5, 2013 07:45
Show Gist options
  • Save shidhincr/7315316 to your computer and use it in GitHub Desktop.
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
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;
};
@ceau
Copy link

ceau commented Sep 26, 2017

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