DEV Community

Discussion on: What s wrong with Array.reduce ?

Collapse
 
kallmanation profile image
Nathan Kallman

I agree your example is one where reduce is better expressed in other ways. But I think there's cases where reduce is the correct metaphor and the clearest construction:

const max = (numbers) => numbers.reduce((max, number) => number > max ? number : max);

I will never be convinced that a for loop is more readable than that (and map and filter aren't options for this type of computation)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I almost always to write first argument as prev or acc; therefore,

numbers.reduce((prev, current) => current > prev ? current : prev)

About readability, I guess it depends on how you visualize.