Skip to content

Instantly share code, notes, and snippets.

@batogov
Last active May 11, 2019 14:11
Show Gist options
  • Save batogov/c6ea322ca4dc77f029d057b30f07e3bb to your computer and use it in GitHub Desktop.
Save batogov/c6ea322ca4dc77f029d057b30f07e3bb to your computer and use it in GitHub Desktop.
Currying [JavaScript]
// Каррирование в JavaScript
function curry(fn) {
// Запоминаем количество аргументов
var arity = fn.length;
return function f1(...args) {
if (args.length >= arity) {
return fn(...args);
} else {
return function f2(...moreArgs) {
var newArgs = args.concat(moreArgs);
return f1(...newArgs);
}
}
}
}
var add = function(a, b, c) {
return a + b + c;
}
var curriedAdd = curry(add);
console.log( curriedAdd(1, 2, 3) );
console.log( curriedAdd(1)(2, 3) );
console.log( curriedAdd(1)(2)(3) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment