There are three problems in your uncurry :
- If not all expected arguments are passed, it returns a function in curry (which is not the behavior of a normal, unconfirmed function)
- he cannot deal with unnecessary arguments
- the implementation is not very functional as you are not reusing anything
Here is a more functional approach:
const id = x => x; const uncurry = f => (x, y) => f(x)(y); const uncurryn = n => f => (...xs) => { const next = acc => xs => xs.reduce(uncurry(id), acc); if (n > xs.length) throw new RangeError("too few arguments"); return next(f) (xs.slice(0, n)); } const sum = x => y => z => x + y + z; try {uncurryn(3)(sum)(1, 2)} catch(e) {console.log(e.message)} console.log(uncurryn(3)(sum)(1, 2, 3)); console.log(uncurryn(3)(sum)(1, 2, 3, 4));
uncurryn ignores unnecessary arguments like any other function in Javascript. It reuses uncurry , reduce and id .
If too few arguments are passed, it throws an error, since in each case it is not clear which value should be returned ( NaN , undefined ).
source share