Hacker News new | ask | show | jobs
by SPBS 2014 days ago
In my experience `map` and `filter` are easy to grok for even for people unexperienced with FP, but `reduce` is always a headache to parse even if you know how to read it. A loop that accumulates some result is almost always better.
1 comments

"reduce" (or fold) certainly takes a while getting used to, but once you gain some intuition for it, it is pretty reusable and it avoids all the problems mentioned above such as off-by one errors etc.

also, if you use map + reduce, it's totally trivial to parallelise the map part and have the main thread combine the partial updates since there is no shared mutable state, e.g. Java also offers parallel streams that support this with basically zero effort. if you use loops, it can be quite hard to rewrite the code so that it is thread safe.