r/coding • u/fagnerbrack • 6d ago
Anecdotally, programmers dislike "reduce"
https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/
133
Upvotes
r/coding • u/fagnerbrack • 6d ago
4
u/andarmanik 5d ago
All reduce is a helper to do the following
{
let accum = init()
for (const el of list)
accum next(accum, el)
return accum
}
96
u/tdammers 6d ago
For me personally, thinking in terms of
mapandfilteris natural, and requires no additional mental overhead (in fact, I find it easier than thinking in terms of imperative loops that achieve the same).I also find thinking in terms of explicit recursion pretty straightforward - what's the base case, and how can you define non-base cases in terms of other cases.
But
reduce/foldjust isn't as intuitive to me, even after decades of functional programming - I often find myself writing the explicit recursion first, notice the pattern, and rewrite it in terms of some flavor offold/reduce. And likewise, when reading code that involves folds, I often unwrap the recursion in my head.My theory is that this has to do with the way
mapandfilterare stateless in a way thatfoldis not. Withmap, you only ever need to worry about the current element and the mapped function, and remember that the result of applying the function to the element becomes the corresponding element in the return value. Withfilter, it's similar - you only need to worry about the current element and the predicate, and remember that the predicate determines whether the element gets included in the output or not. But withfold, each recursion step (or iteration, it doesn't really matter much) depends on the previous (or next) step; you can't just look at each element in isolation, you have to also keep track of what the rest of the input list looks like, and how it has been processed. Just likemapandfilter,reduceabstracts over the plumbing, but unlike those two, the plumbing isn't trivial, and unwrappingreduceinto explicit recursion reveals that plumbing, making it easier to reason about it on a per-item basis.21
u/PHLAK 5d ago
I fucking LOVE reduce! Any time I have a collection of things and need to iterate over them and get to a singular output I reach for reduce.
18
u/thequux 5d ago
map,filter,concatMap, etc are all special cases offold. To wit:The key insight here is that fold is a functional abstraction of a loop. Any loop has some state and a list of items being iterated over. The state starts with some initial value, then on each iteration, the state is updated to consider the next item being iterated over. Finally, once the loop is complete, you have the final value of the state. You can immediately identify each of these elements in the call to fold: the items being iterated over are the list being passed in, the state is the accumulator, and the actions to update the state are the function. It is, you might say, the fully general form of the abstraction.
This leads directly to why fold tends to be less readable than map or filter: any time you apply an abstraction, you are offloading the cognitive effort of understanding part of the code to your pre-existing understanding of the abstraction. With map, you can immediately understand that the output list has a one-to-one correspondence with the input. With filter, you can immediately understand that the output is a subset of the input. Fold, on the other hand, is a purely mechanical transformation of a for/each loop, so it's precisely as difficult to understand as that loop. The abstraction is giving you a lot less cognitive assistance in understanding what the action is doing than a filter or a map.
30
u/EarlMarshal 5d ago
I like to fold and reduce. Where is the problem?
11
u/glenpiercev 5d ago
Reduce is poorly named. What should it be called instead?
4
u/Silent_Marsupial117 5d ago
But reduce/fold are (more?) important:
A tutorial on the universality and expressiveness of fold
Graham Hutton
https://doi.org/10.1017/S0956796899003500
3
u/LemonAncient1950 5d ago
I feel like the problem usually boils down to people trying to use reduce in places where a different approach would be clearer. This is especially true when the reduce callback mutates the result between calls. When it actually fits, it feels right but that usually ends up being boring things like summing a list of numbers -
numbers.reduce((a, b) => a + b, 0)I do remember finding a fun esoteric use for reduce back when JS didn't have async/await. You could get sequential async execution by reducing a promise:
This snippet creates a sequentially executing promise chain. These days you would write this with a regular for/of loop and `await`
7
u/josephjnk 6d ago
I suspect that part of the problem is that reduce is usually used as a way to smuggle imperative code into a functional wrapper. To understand most calls to reduce, you model a series of sequential operations in your head. Whether or not the accumulator is being “modified” via mutation or via copying doesn’t generally change the thought process needed to understand it. It’s inherently less declarative than a map or filter.
Compare this with a function like concatMap, which serves the same use-case, but which I find much easier to read. When you map things into a monoid and “just do what that monoid does” to it you end up back in a declarative world and no longer have to fall back into step-by-step analysis.
2
u/NudeSamoan 5d ago
Yeah, I've only ever used it 12+ years ago while trying to actually get something useful out of MongoDB (which was NOT my choice to use in the first place), so I'll always associate it with stupidity.
1
u/repaj 4d ago
reduceenforces a control flow of reducing on you. Imperative loops gives you more flexibility and they're better to be read. That wouldn't be an issue though, whenever a language would offer some sort of recursion schemes. But I haven't seen any language that does recursion schemes good enough to be more readable than just afor-loop.1
u/kilkil 3d ago edited 3d ago
I used to be somewhat confused by reduce as well, but the example that really cleared it up for me was this:
it basically just means "add repeatedly".
and now, after some practice using it (mostly in Rust, using
Iterator::fold()) I do quite like it!... though I will admit that for loops are still easier to read, especially in languages like Python:
1
u/NewFuturist 1d ago
My experience is that most of the people who use reduce use it incorrectly (e.g. adding fields to an object) and then get upset when you tell them that they shouldn't be using reduce in that situation. Even when you point them to the MDN section about when not to use it, they still don't believe you.
1
u/Mediocre-Brain9051 5d ago
In most cases, reduce is better than otherwise because:
People dislike reduce only when they come from closure-less imperative programming backgrounds...
-9
u/fagnerbrack 6d ago
Here's what you need to know:
Reviewers rarely object when the author uses map and filter, but patches containing reduce often draw comments like "this part is hard to read." He also sees reduce in code far less often than map, filter or some. He offers a few theories: reduce reads harder, feels less familiar, can perform worse than the alternatives, and looks clumsier in JavaScript, Python and Swift. He never got this pushback during his time writing Clojure. He also admits the trend might not be real. He usually just rewrites the reduce call and moves on, even though he prefers it. He has noticed the complaints less lately and suspects code review has become less thorough. He ends by asking readers whether they like reduce.
If the summary seems inacurate, just downvote and I'll try to delete the comment eventually 👍
Click here for more info, I read all comments
-2
u/paul_h 5d ago
Education problem, no?
-2
u/tanjonaJulien 5d ago
Oh men I won't miss this kind of blog post.