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.
-
-
Prikaži ovu nit
-
-
-
(although, in my defence https://developers.google.com/web/fundamentals/primers/async-functions#example_outputting_fetches_in_order …)
Prikaži ovu nit -
Whoa, this is worse than that time I pissed off Trump supporters
Prikaži ovu nit -
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 …Prikaži ovu nit -
BUT, on the other hand, Jimmy-randbro tells me I'm too dumb to appreciate the majesty of reduce, so WHO KNOWS.
Prikaži ovu nit -
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.pic.twitter.com/Be5mCAlTBQ
Prikaži ovu nit -
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).pic.twitter.com/P1okn7edIG
Prikaži ovu nit -
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"pic.twitter.com/97230D4EF2
Prikaži ovu nit -
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.pic.twitter.com/2HLSXxg3gO
Prikaži ovu nit -
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!
pic.twitter.com/w0mnNNvJdf
Prikaži ovu nit
Kraj razgovora
Novi razgovor -
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.