Created
October 31, 2015 16:43
-
-
Save khanghoang/3959c4561d3073f8a50b to your computer and use it in GitHub Desktop.
Currying implementation
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 currying(func) { | |
// how many args that func needs | |
var totalArgs = func.length; | |
var partial = function(args) { | |
return function() { | |
var newArgs = Array.prototype.slice.call(arguments, 0); | |
return fn.apply(null, args.concat(newArgs)); | |
} | |
} | |
var fn = function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
// if still need more args | |
if (totalArgs > args.length) { | |
// recursive to get more args | |
return partial(args); | |
} else { | |
return func.apply(null, args); | |
} | |
} | |
return fn; | |
} | |
module.exports = currying; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment