-
-
Save Integralist/b26afe0a6dfc734298b5 to your computer and use it in GitHub Desktop.
Curry implementation in Node
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
module.exports = curry; | |
function curry (f) { | |
var arity = f.length; | |
var params = []; | |
var end = createEnd(f, arity); | |
return createCurried(params, arity, end); | |
} | |
function createEnd (f, arity) { | |
var src = 'return f('; | |
for (var i = 0; i < arity; i++) { | |
src += (i ? ', p[' : 'p[') + i + ']'; | |
} | |
src += ');'; | |
var endCall = new Function ('f', 'p', src); | |
return function end (p) { | |
return endCall(f, p); | |
}; | |
} | |
function createCurried (collected, arity, end) { | |
return function () { | |
var params = appendArgs(collected, arguments); | |
return params.length < arity | |
? createCurried(params, arity, end) | |
: end(params); | |
} | |
} | |
function appendArgs (arr, args) { | |
return arr.concat(arr.slice.call(args)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment