I've been learning about currying functions and I understood the basic examples I read up on, however I'd really like to understand this example that was given of an advanced function but I'm finding it confusing, if anyone can help break it down that would be great, I understand curry
takes in a function and then this function's arguments are gathered into an array with ...args
but I'm not sure about the logic after or how it's working.
I'd also like to know what the value for (...args)
is and where it is coming from? and what is happening with the line where the function is being bound to args again.
const curry = (fn) => {
return curried = (...args) => {
if (fn.length !== args.length) {
return curried.bind(null, ...args)
}
return fn(...args);
};
}
const totalNum = (x, y, z) => {
return x + y + z
}
const curriedTotal = curry(totalNum);
console.log(curriedTotal(10)(20)(30));