Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@suissa
Forked from kevincennis/curry.js
Created August 14, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suissa/fb9d96a98268ecf6ccbbe7efb0364138 to your computer and use it in GitHub Desktop.
Save suissa/fb9d96a98268ecf6ccbbe7efb0364138 to your computer and use it in GitHub Desktop.
curry.js
function curry( fn ) {
var arity = fn.length;
return (function resolver() {
var mem = Array.prototype.slice.call( arguments );
return function() {
var args = mem.slice();
Array.prototype.push.apply( args, arguments );
return ( args.length >= arity ? fn : resolver ).apply( null, args );
};
}());
}
function volume( w, h, l ) {
return w * h * l;
}
var curried = curry( volume );
curried( 2 )( 3 )( 4 ); // 24
curried( 2, 3 )( 4 ); // 24
curried( 2 )( 3, 4 ); // 24
curried( 2, 3, 4 ); // 24
curried()()( 2, 3, 4 ); // 24
var max10 = curry( Math.min )( 10 );
max10( 12 ); // 10
max10( 6 ); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment