Reputation: 12281
The compose
function in Redux is really simple for the most part and I understand how to use it.
export default function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
However, this one part of the last bit seems really obscure to me:
funcs.reduce((a, b) => (...args) => a(b(...args)))
What is the ...args
part? I understand these are rest parameters but what is it evaluating to? Where is it coming from?
Upvotes: 0
Views: 750
Reputation: 496
Compose is just allowing you to write
compose(func1, func2, func3, func4)
instead of
func1(func2(func3(func4))))
In case of
funcs.reduce((a, b) => (...args) => a(b(...args)))
.reduce is a function of array, you can read about Array Reduce function
It is returning
(...args) => a(b(...args);
and giving you an option to pass some parameters to the nested functions.
for example: It is allowing you to write something like this:
nestedFunc = compose(func1, func2, func3);
nestedFunc({key: value})
which will result in
func1(func2(func3({key: value})))
Read more about compose function
Upvotes: 1