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... [Read Full]
markdown guide
 

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 👏👏

 

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

 

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

 
 

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.

 

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

 

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.

 

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 :)

 

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?

 

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!

 
 

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

 

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

 

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

 

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

 

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

 

Nice succinct article.

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

code of conduct - report abuse