Hi Oliver and thanks for your interesting well written article!
In some of my project where I need it, I like to create a filterMap function which allows me to map values to something else, just like we would with the map and get only truthy values in the JavaScript sense.
The pro of using reduce instead of map combined with filter is mainly performance since the reduce call will do one pass where the map and filter will do it in two passes.
Often, this is a recurring pattern and when I see that I am doing a mapping and a filtering, I know that I can do it in one reduce.
Hi Oliver and thanks for your interesting well written article!
In some of my project where I need it, I like to create a
filterMap
function which allows me to map values to something else, just like we would with the map and get only truthy values in the JavaScript sense.The pro of using reduce instead of map combined with filter is mainly performance since the reduce call will do one pass where the map and filter will do it in two passes.
Often, this is a recurring pattern and when I see that I am doing a mapping and a filtering, I know that I can do it in one reduce.
You are right but it can hurt readability.
Try immutable-js, it has lazy pipeline sequences which work through transducers. No matter how many operations you pipe, only one pass. ☺️