Created
June 24, 2016 15:11
-
-
Save alphaKAI/5f4710d9e9540a507a3b05e386106993 to your computer and use it in GitHub Desktop.
currying in TypeScript! Currying any function.
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 isFunction(obj: any): boolean { | |
return typeof(obj) == "function"; | |
} | |
function curry(func: any) { | |
if (!isFunction(func)) { | |
console.log("Fatal Error. An invalid value was given. It was not a function"); | |
return; | |
} | |
const lamdbdaStr = (lamArgs => | |
((temp) => { | |
for (var i = 0; i < lamArgs.length; i++) | |
temp += "(" + lamArgs[i] + ") => "; | |
return temp + "func(" + lamArgs.join(", ") + ")"; | |
})("") | |
)(iota(f.length).map(x => "arg" + x)); | |
return eval(lamdbdaStr); | |
} | |
console.log("isFunction(curry(add3)) : " + isFunction(curry(add3)));//true | |
console.log("isFunction(curry(add3)(1)) : " + isFunction(curry(add3)(1)));//true | |
console.log("isFunction(curry(add3)(1)(2)) : " + isFunction(curry(add3)(1)(2)));//true | |
console.log("isFunction(curry(add3)(1)(2)(3)) : " + isFunction(curry(add3)(1)(2)(3)));//false | |
console.log(curry(add3)(1)(2)(3) == 6); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment