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

Programming

28506 readers
673 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
top 17 comments
sorted by: hot top controversial new old
[–] KitB@feddit.uk 16 points 4 days 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 3 points 4 days 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 16 points 4 days 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.

[–] qwertyasdef@programming.dev 7 points 4 days 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.

[–] eager_eagle@lemmy.world 9 points 4 days ago

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

[–] FluidBeef@quokk.au 5 points 4 days ago (2 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 6 points 4 days 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 4 days ago

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

[–] vk6flab@lemmy.radio 5 points 4 days ago (1 children)
[–] cactusupyourbutt@lemmy.world 6 points 4 days ago

thats perfectly readable to me

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

Found my brave and true friend.

[–] setsubyou@lemmy.world 2 points 4 days 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 4 days ago* (last edited 4 days 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, ...

[–] galaxy_nova@lemmy.world 1 points 4 days 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

[–] Feyd@programming.dev 1 points 4 days ago* (last edited 4 days ago)

I like it ¯\_(ツ)_/¯

[–] tabular@lemmy.world 1 points 4 days 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.