this post was submitted on 15 Sep 2026
34 points (100.0% liked)

Programming

28954 readers
548 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 3 years ago
MODERATORS
all 19 comments
sorted by: hot top controversial new old
[–] KitB@feddit.uk 18 points 1 month ago (1 children)

Reduce is harder to read. Maps and filters are deeply simple. The problem space a reduce can solve is, I'm pretty sure, strictly larger than both of them. Its generality is its downfall.

I'll happily use a reduce, but it has to be either as a well named function or immediately stored in a well named variable. If the reduce is complex enough that you can't name it, that's a smell to me, reach for a different tool.

[–] mcv@lemmy.zip 4 points 1 month ago

Absolutely. I've used many reduces that I later regretted. It's powerful, but also too cryptic.

And reduce has hidden performance risks. I've seen reduce used where the collector was assembled with a spread operator. But that's a loop within a loop, and can get very expensive. A simple forEach loop and just appending the new value in old fashioned ugly pedestrian imperative code, does not carry that risk.

[–] LeapSecond@lemmy.zip 17 points 1 month ago

I like the concept of reduce and in many cases a different implementation is not necessarily more readable. But I hate how I have to look up the syntax every time, in every language I've written. I don't know if that's a me problem though.

[–] eager_eagle@lemmy.world 10 points 1 month ago

I don't hate it, but I prefer not to use it mostly for readability

[–] qwertyasdef@programming.dev 7 points 1 month ago

I like reduce if the reduction function is super simple, but the common cases usually already have their own specialized functions (sum, max, average, etc) so I rarely need reduce itself. The only times I can think of are concatenating a stream of strings in Java, and bitwise-or-ing a bit array into an integer bitset in JS.

Anything more complicated and I reach for a loop. Especially if the aggregated value is itself some kind of collection.

[–] FluidBeef@quokk.au 6 points 1 month ago (3 children)

Isn’t reduce just aggregation operations such as count, sum, etc? What’s even the alternative there if someone disliked doing aggregation for some reason? What if I anecdotally disliked subtraction?

[–] verstra@programming.dev 7 points 1 month ago

It's aggregation in general. It's the iterator function you can use to implement sum, or count, or both at the same time.

This post is just saying that's interesting that people dislike is more than say map or filter

[–] locuester@lemmy.zip 2 points 1 month ago

If u don’t like subtraction you can add after multiplying by -1

[–] cyclohexane@lemmy.ml 2 points 1 month ago

The alternative most people probably use is iterative loops, like for loops and while loops and the like, with a mutable variable somewhere. I don't love it, personally.

I guess the other potential alternative is doing the iteration recursively, but I doubt that's what most people think of as an alternative to reduce.

[–] vk6flab@lemmy.radio 6 points 1 month ago (2 children)
[–] cactusupyourbutt@lemmy.world 9 points 1 month ago

thats perfectly readable to me

[–] Ephera@lemmy.ml 5 points 1 month ago

Yeah, it sounds really abstract until you see that summation example. Then it's suddenly really obvious...

[–] somegeek@programming.dev 5 points 1 month ago (1 children)
[–] Wiz@midwest.social 3 points 1 month ago

Found my brave and true friend.

[–] setsubyou@lemmy.world 2 points 1 month ago (1 children)

I don’t think it’s really less elegant in Python or Swift. Especially not in Swift where you can write it very concisely in some cases, like numbers.reduce(0, +). Admittedly Python is a bit more verbose.

But maybe Python and Swift programmers are not used to it in the same way as Clojure programmers are.

[–] eager_eagle@lemmy.world 2 points 1 month ago* (last edited 1 month ago)

also python has built-ins for sum, min, max, and math.prod - which account for a lot of use cases of reduce in a more readable way.

But if you're really crunching numbers, it's just better to go with something like numpy, pandas, polars, ...

[–] tabular@lemmy.world 1 points 1 month ago

I cannot recall needing Reduce while writing in GDscript (imperative language for the Godot game engine). I find use for Filter and Any easily, and use Map when trying to write functional code. I assume using Map is harder to read for fellow Godot users, so Reduce is probably a total unknown - it's not the normal way to code in that enviroment.

[–] galaxy_nova@lemmy.world 1 points 1 month ago

I’ve used reduce a decent bit to write som expressions in pyspark without being as verbose so I must say I definitely don’t hate it