return broReduce(reducer, reducer(initial, arr[index]), arr, index + 1);
Eric Elliott, thanks for the great post!
Alexander Sobolev

This is how I might have written it, if only JavaScript had good support for proper tail calls.

Sadly, proper tail calls got added to Safari, but no other major browser has implemented them.

What that means is that for very large collections, or or even medium-sized collections on very memory-limited devices, broReduce() will cause a stack overflow and your program will crash. );


I addressed recursion in an earlier article in this series. You should probably start at the beginning and read the whole series. It’s worth it, in my opinion, but I may be biased.

I personally find the recursive version very clear and easy to read, but I wrote Lisp for years, so that doesn’t mean everybody will share my love of recursion.

I also find that it’s better to adopt a culture of mentorship and train the team up than it is to dumb the code down.

That doesn’t mean you should be writing totally cryptic code, but it does mean you shouldn’t be afraid of a little functional programming.

Why don’t you start by sending everybody on the team a link to this series? ;)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.