November 2017
Intermediate to advanced
386 pages
9h 22m
English
Let's examine yet another way of doing partial application, which will behave in a fashion somewhat reminiscent of the curry() functions we wrote earlier in this chapter, and solve the deficiency we mentioned at the end of the previous section:
const partialByClosure = (fn, ...args) => { const partialize = (...args1) => (...args2) => { for (let i = 0; i < args1.length && args2.length; i++) { if (args1[i] === undefined) { args1[i] = args2.shift(); } } const allParams = [...args1, ...args2]; return (allParams.includes(undefined) || allParams.length < fn.length ? partialize : fn)(...allParams); }; return partialize(...args);};
Wow, a longish bit of code! The key is the inner function partialize(). Given a list ...
Read now
Unlock full access