DEV Community

Discussion on: What s wrong with Array.reduce ?

Collapse
 
johnkazer profile image
John Kazer

The problem with reduce is that there are two things happening in parallel. What happens to the accumulator and the function operating on the list. This situation always makes reasoning harder, at least until you've practised a bit.

I guess you can say a for loop has same issue with the iterator and the operations carried out but they are sort of separate things.

The reduce example in the article added a third piece of action, the choice between valid and invalid. To me that is too many moving parts for a functional approach and the latter example using two filters is better.

Yes it is slower with the two list iterations but if you need speed then use a for loop.

Collapse
 
dvddpl profile image
Davide de Paolis

I really appreciate how you articulated your thoughts. This is how I see it too. Thanks