Last active
August 29, 2015 14:16
-
-
Save dclowd9901/00fc25d48103e9c82cce to your computer and use it in GitHub Desktop.
Currying
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 duplicate(arr) { | |
return arr.concat(arr); | |
} | |
function add(a, b) { | |
return a + b; | |
} | |
function argToArray(args) { | |
var arr = [], | |
i = 0, | |
len = args.length; | |
for (; i < len; i++) { | |
arr.push(args[i]); | |
} | |
return arr; | |
} | |
function curry(func) { | |
var argsArray = argToArray(arguments).slice(1,0), | |
arity = func.length; | |
return function curried() { | |
argsArray = argsArray.concat(argToArray(arguments)); | |
if (argsArray.length === arity) { | |
return func.apply(this, argsArray); | |
} else { | |
return curried; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment