-
-
Save suissa/fb9d96a98268ecf6ccbbe7efb0364138 to your computer and use it in GitHub Desktop.
curry.js
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 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 ); | |
}; | |
}()); | |
} |
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 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