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;
};
@ax-hd
Copy link

ax-hd commented Sep 5, 2017

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

@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