Last active
May 11, 2019 14:11
-
-
Save batogov/c6ea322ca4dc77f029d057b30f07e3bb to your computer and use it in GitHub Desktop.
Currying [JavaScript]
This file contains 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
// Каррирование в 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