r/coding • u/fagnerbrack • 16d ago
Anecdotally, programmers dislike "reduce"
https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/
138
Upvotes
r/coding • u/fagnerbrack • 16d ago
View all comments
Show parent comment
6
u/tdammers 15d ago
Reduce/fold is also more general - you can implement both
mapandfilterin terms offold, but not the other way around, andfoldcan do things thatmapandfilteron their own or together cannot.In Haskell (using right folds; left folds are also possible, but slightly less elegant):
And here's an example of a function that can be implemented using
foldr, but not a combination ofmapandfilter:sum. Again in Haskell (simplified to only support integers):Try implementing that using
mapand/orfilter: you can't, not without introducing explicit recursion/iteration anyway. And that's exactly because neithermapnorfiltergive you access to the state accumulator, butfoldrdoes. You can even see this in the type of their respective first arguments:maptakesa -> b(take current element, return its replacement),filtertakesa -> Bool(take current element, return whether it should be included or not), butfoldrtakesb -> a -> b(take accumulator and current element, return updated accumulator).And I think this "accumulator" bit is the part that makes it more complex, more general, more abstract, and more difficult to visualize.