The way I see it, it's mostly familiarity bias. reduce will be harder to understand to someone who is not used to it and, well, since anything that can be done with reduce can be done with a for loop, why learn a new construct?
Also, regarding your example, I think a better functional primitive to use here would be partition. Unfortunately, Javascript doesn't have partition out of the box, but it's a function that takes a predicate and a list and returns a list with all the elements for which the predicate evaluates to true and another with all the elements for which the predicate evaluates to false.
The implementation is straightforward with reduce:
This is to show that, while reduce is super useful, there are still a lot of functional primitives missing in Javascript, which forces people to use less elegant solutions, and might contribute to the perception of reduce being hard to read.
I created a dev.to account because examples like these are mind boggling. It seems to me that reduce works great for the mathematically-minded and can parse that function very easily, but have to ever stopped and thought about the alternative for-loop? Because to me this looks wildly more readable and it's guaranteed to be more efficient too (no array creation at every loop)
The way I see it, it's mostly familiarity bias.
reduce
will be harder to understand to someone who is not used to it and, well, since anything that can be done withreduce
can be done with a for loop, why learn a new construct?Also, regarding your example, I think a better functional primitive to use here would be
partition
. Unfortunately, Javascript doesn't havepartition
out of the box, but it's a function that takes a predicate and a list and returns a list with all the elements for which the predicate evaluates to true and another with all the elements for which the predicate evaluates to false.The implementation is straightforward with
reduce
:Or you can take it for instance from ramda.
And then your
analyzeResults
is just a one liner:This is to show that, while
reduce
is super useful, there are still a lot of functional primitives missing in Javascript, which forces people to use less elegant solutions, and might contribute to the perception ofreduce
being hard to read.very good point. thanx for commenting
This!! Yes! I actually used reduce in JS and Ruby to partition in the past
I created a dev.to account because examples like these are mind boggling. It seems to me that reduce works great for the mathematically-minded and can parse that function very easily, but have to ever stopped and thought about the alternative for-loop? Because to me this looks wildly more readable and it's guaranteed to be more efficient too (no array creation at every loop)