-
-
Save atinder/0db9ae08f0baade25e9987019ca54692 to your computer and use it in GitHub Desktop.
ES5 vs ES6 function 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 curry(fn) { | |
var args = [].slice.call(arguments, 1); | |
function curried(fnArgs) { | |
if (fnArgs.length >= fn.length) { | |
return func.apply(null, fnArgs); | |
} | |
return function () { | |
return curried(fnArgs.concat([].slice.apply(arguments))); | |
}; | |
} | |
return curried(args); | |
} |
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 curried(fn, ...args) { | |
var curry = (fnArgs) => { | |
if(fnArgs.length >= fn.length) { | |
return fn.apply(this, fnArgs); | |
} | |
return (...cArgs) => curry([...fnArgs, ...cArgs]); | |
} | |
return curry(args); | |
} | |
var add = curried(function(a,b){ return a + b;}); | |
var increment = add(1); | |
// (...cArgs) => curry([...fnArgs, ...cArgs]) | |
increment(4); | |
// 5 | |
add(1, 2); | |
// 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment