All code using array.reduce should be rewritten without array.reduce so it's readable by humans *mutes thread*
Ok ok, fair enough, there are cases where array.reduce is the best thing to use.

Specifically, cases where you want to look really smart and don't care about readability.
I mean, wtf:
wait
Whoa, this is worse than that time I pissed off Trump supporters
Here's a great quote from the inventor of Python (h/t @tigt_):

"the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly"

https://www.artima.com/weblogs/viewpost.jsp?thread=98196
BUT, on the other hand, Jimmy-randbro tells me I'm too dumb to appreciate the majesty of reduce, so WHO KNOWS.
Ok, I've been asking folks for good examples of reduce, and they fall into a few categories. Here's what I've seen:

SUMS! Yes, this is the textbook example, and one of the few ok uses of reduce. The reduce example is probably better here.
Folks suggested other forms of 'gathering', such as concatenating the keys of an object.

In this case I think the map + join is more expressive. However, I'd go for the loop if the two iterations caused performance issues (which is unlikely).
Another interesting example was "getting the size of the largest string". Again, I find the map + max version better describes the intent.

"Is the length of the first item bigger than the biggest item so far? If so, remember it"

vs

"Get all the lengths, give me the biggest"
Most suggestions were object transformations. I strongly believe these should not use reduce.

Most were creating the object once, then passing the same object back through reduce. This is using reduce for decoration only. The only part of reduce it's using is the looping.
I saw ones similar to the above, but the author had realised the reduce was being used for decoration. So to avoid that, they create a new object on each iteration.

This is not only obfuscated, it's O(n2) slow.

It might make you look smart to other developers though! 😀
You can follow @jaffathecake.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: