Hey Joshua, thank you for the comment and I'm happy you like the article.
To quickly answer your question, of course don't 'need' to do it with partials but they are great advantages in using that approach. Let me try to give you a quick example as a basic use case.
Let's say we have a multiplier function that takes two arguments and returns the product. We will use that as our base
function multiply(x, y) {
return x * y;
}
Now let's say we have the need to calculate often halves, doubles and triples. Partial function application will allow us to create generic, reusable, predictable and intuitive secondary functions as you can see below
const halves = multiply.bind(null, 0.5);
halves(2);
const doubles = multiply.bind(null, 2);
doubles(5);
const triples = multiply.bind(null, 3);
triples(27);
You get the idea. I will take a closer look at your solution here and give you some feedback on it. But in an interview context it might be best to solve against the specs first then propose a better alternative.
Let me know what you think.
Cheers
Hi,
Great Article!
I'm just confused with this instruction on #3. Calculate Employee Gross Pay
I don't think it needs
partial applicationfor calculating those 3 roles since the base functions acceptsroleobject already. But it can help to createpartial applicationwhen computing the grossPay for the total of hourly and overtime.const getGross = (rate) => (totalHours) => rate * totalHours const computeGrossSalary = ({ role = {}, totalHours = 0, overtimeTotalHours = 0 }) => { const { rate: hourlyRate = 0, overtime: overtimeRate = 0 } = role || {} const totalHourlySalary = getGross(hourlyRate) const totalOvertimeSalary = getGross(overtimeRate) return totalHourlySalary(totalHours) + totalOvertimeSalary(overtimeTotalHours) } computeGrossSalary({ role: { roleId: 3, rate: 12.5, overtime: 18.75 }, totalHours: 1 })Please correct me if am wrong.
Cheers!