Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created June 24, 2016 15:11
Show Gist options
  • Save alphaKAI/5f4710d9e9540a507a3b05e386106993 to your computer and use it in GitHub Desktop.
Save alphaKAI/5f4710d9e9540a507a3b05e386106993 to your computer and use it in GitHub Desktop.
currying in TypeScript! Currying any function.
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