DEV Community

Cover image for The Holy Trinity of Functional Programming: Map, Filter and Reduce

The Holy Trinity of Functional Programming: Map, Filter and Reduce

Mikhail Levkovsky on August 17, 2019

You've watched the Youtube videos, you've done the tutorials on Pluralsight and you consider yourself a Javascript expert. However, now you want t...
Collapse
 
hellokyyt profile image
Kyle Harrison • Edited

What I like about this article, is discussing the virtues of why you'd want to use these functions, but juxtaposed with commentary about their weaknesses and why you'd maybe still want to keep the "old" stuff in your toolbelt. Bravo 👏👏

Collapse
 
mlevkov profile image
Mikhail Levkovsky

much appreciated :)
thanks for taking the time to read it

Collapse
 
jejones3141 profile image
James Jones

There is the monotheistic school that recognizes that map, filter, and reduce are all instances of folding. :) Seriously, good article!

Collapse
 
mlevkov profile image
Mikhail Levkovsky

Much appreciated :)

Collapse
 
chrisachard profile image
Chris Achard

Yes! Really understanding map, filter and reduce really helped as I would read other people's code, and also really helped clean up my own code (especially when writing React and Redux).

Another note about .reduce: you can also reduce into an object or array as well - so look into that if you want to transform one object/collection into another one.

Collapse
 
mlevkov profile image
Mikhail Levkovsky

It's so cool, I use it in my day to day and shorten my functions so much just by introducing these nice high order functions.

Reduce is so powerful! You can compose functions like crazy and just make beautiful poetry with your code. I didn't include it here because it's more of an intro post but would be great to make a more advanced reduce article

Collapse
 
priitpu profile image
Priit • Edited

You put emphasis on the fact that map, filter and reduce go through every single element of the array, that being a theoretical con, compared to a for-loop. Yet your examples don't showcase the use-cases of breaking/continuing so currently the examples are more verbose versions of native methods.

Also for-of loop !== for loop.

Otherwise a good intro to more abstract methods.

Collapse
 
mlevkov profile image
Mikhail Levkovsky

Appreciate the feedback, good point that I didn't dig in how to break out of the for loop. I assumed people knew how to do it but thats maybe a faulty assumption.

Good point about the for-of loop !== for loop, will be more careful in the future :)

Collapse
 
morgboer profile image
Riaan Pietersen • Edited

Great succinct article, Mikhail, thank you for writing it.

Question about your .filter example.
You say that using .filter will loop through everything and create another array that contains the result set.
Then, in the forloop example, you are creating a separate array in any case (const result: User[] = []).

As I see it your forloop approach should avoid creating another array - right? What would the approach be to not create a result set array?
In the past I have avoided creating a duplicate by slicing the unmatched value out of the original array. Would that work?

Collapse
 
anthonybrown profile image
Tony Brown

would you say that these higher order functions are just wrappers for loops?

a friend of mine said that and it threw me off because I thought loops were synchronous and using something like the map function was an asynchronous way of coding?

Great article!

Collapse
 
adipolak profile image
Adi Polak

short, simple and to the point! thank you for supporting a more functional world!

Collapse
 
mlevkov profile image
Mikhail Levkovsky

thank you for taking the time to read :)
much appreciated

Collapse
 
josephthecoder profile image
Joseph Stevens

Spread the FP word! 💯💯

Collapse
 
ajinspiro profile image
Arun Kumar

Ugh... I think u accidentally copy pasted code for filter in reduce...

Collapse
 
mlevkov profile image
Mikhail Levkovsky

I don't think I did, they just look really similar. Instead of .filter it says .reduce :)

Collapse
 
ajinspiro profile image
Arun Kumar

Apologies, at the time I "really" saw filter instead of reduce. LOL

Collapse
 
jimtomlinson profile image
Jim Tomlinson

Nice succinct article.

Code typo: in the for-loop version of getActiveUsers(), you want to return result, rather than users.