A simple impl in Typescript...
function doCurrishSortOfThingToIt(fn: Function) { const savedArgs: any[] = []; return curriedFn; function curriedFn(...newArgs: any[]) { savedArgs.push(...newArgs); if (newArgs.length) return curriedFn; const result = fn.apply(this, savedArgs); savedArgs.length = 0; return result; } }
Running though the examples...
function adder () { return [].slice.call(arguments).reduce(function(a,b){ return a + b },0); } var curryAdder = doCurrishSortOfThingToIt(adder); curryAdder(1); curryAdder(1,2,3); curryAdder(2)(2,5); var example = curryAdder(); curryAdder(1)(2); var example2 = curryAdder(); console.log(example, example2) // output: 16, 3 var curryEval = doCurrishSortOfThingToIt(eval); curryEval("var y = function(){return true}"); curryEval(); console.log(y()); // output: true
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A simple impl in Typescript...
Running though the examples...