DEV Community

Discussion on: ES6 - Higher-Order Functions

Collapse
 
pentacular profile image
pentacular

Actually, I don't think that I'd consider this to be simpler or more readable.

const totalProfit = marvelMovies.reduce((accumulator, movie) => {
    return accumulator + movie.profit;
}, 0);

The problem with reduce is that it is a general purpose tool, with boilerplate baggage, so it carries almost no meaning in itself, and needs decoding.

In other words, it carries the wrong level of abstraction for direct use.

On the other hand, this would be simpler.

const sum = (array, accessor) => array.reduce((sum, value) => sum + accessor(value), 0);
// ...
const totalProfit = sum(marvelMovies, movie => movie.profit);
Collapse
 
garretharp profile image
Garret

I personally prefer the way in which the article is written. Sure the word sum may sound nicer for this very specific case of array reduce, but you are unnecessarily making a function that does not need to be there.

Collapse
 
skaytech profile image
skaytech

Like I said, it's a personal preference..

Collapse
 
skaytech profile image
skaytech • Edited

Hi Pentacular,

Thanks for reading the article. I believe I have translated the explanation on how to use the reduce function as closely as possible to the MSDN docs.

Reduce is one function that everyone has varying opinions. Your examples have definitely made the syntax shorter using ES6 shorthand techniques.

In my humble opinion, the term 'Simpler' is very relative. What maybe simple to you might not be to othrrs. Having said that, I always welcome other ways to do the same thing without attaching any tags to it 🙏.

Please continue to share your views and thanks once again for reading my article. Cheers!!