-
-
Save sayanriju/b0bec74f55109596266feae5a5b908cb to your computer and use it in GitHub Desktop.
curry.js
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 curry( fn, arity ) { | |
//var arity = fn.length; | |
return (function resolver() { | |
let mem = Array.prototype.slice.call( arguments ); | |
return function() { | |
let args = mem.slice(); | |
Array.prototype.push.apply( args, arguments ); | |
return ( args.length >= arity ? fn : resolver ).apply( null, args ); | |
}; | |
}()); | |
} |
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
let multN = function(...args) { | |
return args.reduce( (acc, cur) => acc * cur, 1 ) | |
} | |
let mult3 = curry(multN, 3) // specify arity as 2nd argument | |
let mult4 = curry(multN, 4) // specify arity as 2nd argument | |
console.log( mult3(1,2,3) ) // 6 | |
console.log( mult3(1,2)(3) ) // 6 | |
console.log( mult3(1)(2)(3) ) // 6 | |
console.log( mult4(1)(2)(3)(4) ) // 24 | |
console.log( mult4(1,2)(3)(4) ) // 24 | |
console.log( mult4(1,2,3,4) ) // 24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment