r/coding • • 18d ago

Anecdotally, programmers dislike "reduce"

https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/
138 Upvotes

77 comments sorted by

View all comments

11

u/glenpiercev 17d ago

Reduce is poorly named. What should it be called instead?

11

u/Abs0rbed 17d ago

Rust has `collect` which is basically just a reducer

1

u/Ok-Argument7176 16d ago

How is that?

1

u/funkdefied 15d ago

Rust’s collect returns a collection, not a single value like reduce. Very different in terms of approachability imo. 

1

u/Abs0rbed 15d ago

The collection is the accumulator returned from the reduce operation on the iterator

1

u/AlxandrHeintz 15d ago

It doesn't actually. It returns a FromIterator - which may (and typically is) be a collection. But you can collect into anything that supports it. For instance, if you have an iterator of () you can collect that into just ().

1

u/kilkil 15d ago

no, rust's equivalent to reduce are Iterator::reduce() and Iterator::fold() (as well as a few other variants).

I guess you could argue collect() does something similar, but it's a lot more specialized. you can't "collect" a Vec into a sum, for example.

1

u/AlxandrHeintz 15d ago

You can actually. If you make a struct Sum(usize) and implement FromIterator<usize>, you could do let sum: Sum = my_iter.collect();.

Edit

I'm not saying this is a good idea btw, just that it's possible.

1

u/Abs0rbed 15d ago

It wasn’t a perfect example, I just meant that I often think about reduce as something that “collects” a stream of values rather than “fold”. I just think collect is a more intuitive word for it

→ More replies (1)

6

u/ummaycoc 17d ago

It's from (at least as far as I've read) APL where / (the reduce operator) to convert an n+1 dimensional value into an n dimensional value, thus reducing the dimension by one. Since you can use it in JavaScript/etc to take a vector and yield a vector it is definitely not appropriate.

values ← 2 3 2 ⍴ ? ⍳ 12

values

1 2
3 2
2 2


7 2
1 1
1 3

+/ values

3 5 4
9 2 4

4

u/BarelyAirborne 17d ago

APL is still my favorite programming language. Thanks for the nostalgia.

2

u/ummaycoc 17d ago

Mine too.

3

u/Baby_Fark 17d ago

Agreed. Maybe like ‘Combine()’?

3

u/lookmeat 16d ago

aggregate, which takes an aggregator function and an aggregate (which represents the initial state). It feels, IMHO, more intuitive.

And for unfold I think that build or construct where it takes a function that knows how to build a piece based on some blueprint by splitting it into smaller blueprints. So it works like a parser, but also like your standard builder if you think of the "fluent API" as assembling the blueprint object.

5

u/wnoise 17d ago

foldl

1

u/tb5841 17d ago

Haskell calls it foldl or foldr, depending on which way you're going.

1

u/tumes 16d ago

Big ups to ruby’s inject (which is just an alias for reduce, which also works). I don’t actually think it’s better tho, it’s not expressive in the way that hints at what it does, but it is a little more expressive if you already know what it does, which… ok? It is weird though and I like it for that reason alone.

1

u/cdsmith 16d ago

Reduce is named just fine for an associative fold. For non-associative folds, the concept is general enough to not have a good descriptive name, so you can settle on whatever abstract name you like.

1

u/Stanian 16d ago

"aggregate"?

1

u/KontoOficjalneMR 15d ago

What should it be called instead?

accumulate

1

u/kilkil 15d ago

well it kind of "folds" an array in on itself, so maybe it should be called fold... oh wait

1

u/Alpheus2 14d ago

Collect, fold, project. Pick two