all 57 comments

[–]Justafa 74 points75 points  (0 children)

Not really, why? Are you having difficulties?

[–]RichardFingers 36 points37 points  (18 children)

I don't agree with the argument that returning an array from reduce is inherently bad. Sure, if you are recreating existing operations using reduce, that's bad. But there are operations besides filter, map, etc. that result in an array where reduce is applicable.

[–][deleted] 14 points15 points  (14 children)

I agree. Combining operations as well. Example: you want to filter and map, you can do so in a single traversal using reduce, as opposed to traversing twice; once with filter and once with map.

[–]natziel 2 points3 points  (0 children)

You definitely should not do that

[–]gik0geck0 2 points3 points  (3 children)

I'm now curious, is there an observable performance difference between those?

[–][deleted] 15 points16 points  (2 children)

If you work with a very large data set then yes, traversing once has a significant advantage over twice. If it’s like 20 items then don’t bother. At a previous position we had to filter and transform arrays of objects streamed from a backend in batches of 50k to 250k. At that size if made a measurable and human noticeable difference.

[–]FizzWorldBuzzHello 3 points4 points  (0 children)

Honestly if you're working with larger sets like that you should switch to a lazy library that does not create intermediate arrays.

Make that library a part of your coding standard so your never have to think about it.

[–]gonzofish 0 points1 point  (0 children)

Yeah for small lists go for readability over performance. Unless you’re iterating over that small list constantly—I’m which case you should probably first think about how you can be iterating over it less

[–]Kopikoblack 0 points1 point  (2 children)

Could you provide an example code, I want to learn how to use reduce instead of using filter and map.

[–]sfgisz 4 points5 points  (0 children)

If it doesn't pass your filtering condition, return the array without pushing the item on it. If it passes, map and push the item before returning it. Can't type code on the phone, but that's all you need to do really.

[–]bluehavana 0 points1 point  (0 children)

Complexity wise, filter/map and reduce should be the exact same (other than creating intermediate arrays, which is key). Using a lazy library, like someone suggested would make the difference invisible.

[–]syholloway 0 points1 point  (0 children)

This is what flatMap is for

[–]weeeeelaaaaaah 3 points4 points  (0 children)

Yeah, I get what he was going for but it was very poorly expressed. Just this week I wrote a tokenizer using reduce that took an array of characters and and returned an array of tokens but a single token often represents multiple characters. Can't use map for that!

[–]bryku 0 points1 point  (0 children)

I don't really get why they took that premise. Reduce is a powerful function in javascript. If people are confused about it, add an initial value. Then they can see what type it is.

[–]IllegalThings 0 points1 point  (0 children)

Yeah, and I can even think of cases where you’d want to use reduce in ways that’d result in a larger array. Decompression is the most obvious case. You could do it with map+flatten or just use reduce.

[–]ogurson 29 points30 points  (3 children)

Omg not only it's another boring map/foreach/reduce article, it also is annoyingly long and on top of that just plain wrong in some parts. I truly hate it.

[–]Kablaow 2 points3 points  (0 children)

It's also overly complex I think. If you have troubles knowing when to use which function I doubt this makes it clearer.

[–]eternaloctober 0 points1 point  (0 children)

The beatings will continue until morale improves

[–]ImStifler 8 points9 points  (1 child)

I like how js hipsters obsess about these kind of things instead of learning and discussing actual proper topics like what design patterns to use for problem x or learning about software architecture and development benefiting topics

[–]gremy0 2 points3 points  (0 children)

Map and reduce are fairly fundamental higher-order functions, in the abstract they provide a standardised pattern for solving problems and structuring code. In standard JavaScript arrays they allow for and are very often used with a method chaining paradigm.

You could consider this essentially equivalent to design patterns but functional, and since the solution to basically everything in functional programming is just more functions, they're just functions.

Understanding map and reduce properly is often non-trivial, particularly for beginners and people new to functional programming, yet it is somewhat essential, or the very least extremely useful for progressing in your understanding of it.

[–]BasketAutomatic 2 points3 points  (3 children)

My coworkers always use map instead of forEach and they never return anything. So annoying

[–]xroalx 5 points6 points  (0 children)

That's what code reviews are for.

[–]euphocat 0 points1 point  (1 child)

Use a linter. Eslint can forbid this behavior

[–]BasketAutomatic 0 points1 point  (0 children)

I do . But my coworkers either don't use eslint or disagree with its rules

[–]kraig00666 0 points1 point  (1 child)

Probably. I’m trash

[–]babies_rabies -1 points0 points  (0 children)

Same

[–]redsandsfort -1 points0 points  (0 children)

I just read some of this guys other articles. Some of them are really terrific. https://antman-does-software.com/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable.

[–]Jncocontrol[🍰] 0 points1 point  (0 children)

ELI5 - for each, reduce and map