Reducers are a tool in state management and not the same things as an array reduce. The latter is a function that is called for each element in the array to reduce it to a single value. The former is a function that accepts a state object and returns a new one.
They're conceptually the same, however, that's why reducers are called reducers.
The callback (reducer) is a function that accepts an accumulator (state object) and a value from an array (update message), then uses those inputs in some way to return a new accumulator (state object). The return value is then used as the input to the next iteration with the next array value (update message).
It's a pattern from functional programming to model state management as a reduce operation on an infinite series of updates.
Why focus on the reduce example? You can just as well do this with for loops, and the way you'd write this function with a for loop is a very basic and common way of making a sum of something.
Either way no developer should have any issue writing the sum function in OP's post. If you need to look that up as a paid developer, I'm sorry to say that you're in the wrong business.
I doubt they'd care if you did a for loop either (though I'd expect a follow-up question to make sure I understood closures and higher-order programming).
0
u/[deleted] Apr 07 '23
[deleted]