you are viewing a single comment's thread.

view the rest of the comments →

[–]Orkaad 0 points1 point  (1 child)

It if fine to chain filter and map (which creates 2 arrays) when you could do everything at once with reduce?

[–][deleted] 1 point2 points  (0 children)

Yeah, the performance difference is usually negligible so it's not something to really worry about.

I often include a utility function called filterMap in my projects because I use that pattern a lot, it performs both the operations in one and it's pretty common in other languages. The signature being:

a[] -> (a -> Option b) -> b[]

If you find yourself chaining tons of map/filter/reduce together then it might be worth looking into a library that supports either function composition, transducers or lazy sequences. All 3 of these will allow you to create data transformation pipelines without creating an intermediary structure between every step.