• qwertyasdef@programming.dev
    link
    fedilink
    arrow-up
    7
    ·
    2 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.

  • KitB@feddit.uk
    link
    fedilink
    English
    arrow-up
    16
    ·
    2 days ago

    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
      link
      fedilink
      arrow-up
      4
      ·
      2 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
    link
    fedilink
    arrow-up
    16
    ·
    2 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.

  • FluidBeef@quokk.au
    link
    fedilink
    English
    arrow-up
    4
    ·
    2 days ago

    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
      link
      fedilink
      arrow-up
      6
      ·
      2 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

    • Cyclohexane@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      2 days 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.

  • galaxy_nova@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 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

  • setsubyou@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    2 days ago

    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
      link
      fedilink
      English
      arrow-up
      2
      ·
      2 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, …

  • tabular@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 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.