Last active
September 10, 2021 06:46
-
-
Save Sidd27/c483bca349214f94ecf98283da6c4ce2 to your computer and use it in GitHub Desktop.
Curry Function in ES5 (Javascript) It returns the curried function
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
function curry(fn) { | |
var arity = fn.length; | |
return (function resolver() { | |
var memory = Array.prototype.slice.call(arguments); | |
return function () { | |
var local = memory.slice(), | |
next; | |
Array.prototype.push.apply(local, arguments); | |
next = local.length >= arity ? fn : resolver; | |
return next.apply(null, local); | |
}; | |
}()); | |
} | |
const es6Curry = (f, arr = []) => (...args) => ( | |
a => a.length === f.length ? f(...a) : es6Curry(f, a) | |
)([...arr, ...args]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment