‹ BackHN Continuity

Thread

Anecdotally, programmers dislike "reduce"

175 points · 283 comments · vinhnx

  1. catapart · · focus · HN ↗
    For me it's the name[0]. map puts out an array that has been mapped from another array. filter puts out an array that is a filter of the input array. both of those are always true. reduce, on the other hand, may put out a reduction of the input array (probably most of the time), but the fact that it may not means that what is happening is not actually a reduction. In languages like js/ts, you don't even have to return anything of the same type as the input array's elements. You could literally "reduce" and array of integers to a cancellation token, or a state object, or anything else.

    I realize it's not the most efficient way to work, but I like my code to read like instructions. There's nothing reduce will do that a for loop won't accomplish and the for loop (+ an accumulator, of course) is more clearly "readable" than reduce. If I read map, I know what's going on. If I read filter, I know what's going on. If I read reduce, I have to figure out what's going on, even if I'm pretty sure what is going on. If I could rely on reduce to always give me back an element of the input array, I would use it more. But since it can give back anything, I prefer the simplicity of a for loop.

    [0] I don't have any suggestions for "better" names because the whole operation is hard to sum up in a word? "dispatch" makes sense, as a function dispatching a function over each element in an array, but it masks the concept of accumulation from return values. "transform" is accurate, but hardly descriptive at all. the list goes on. It's an undeniably useful little function, it's just hard to make it easy to understand and therefore debug.

    1. adamddev1 · · focus · HN ↗
      I like the name `fold` as used by Haskell, Racket, et al. It gives me an image of folding up a long list into a ball, one chunk at a time.
      1. anitil · · focus · HN ↗
        I like fold too, but I can never remember foldl vs foldr, it's always backwards to what I expect somehow
        1. internet_points · · focus · HN ↗
          <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49736033">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49736033 may help :)
          1. anitil · · focus · HN ↗
            Thanks! That visual style approach works better for me, hopefully it sticks
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.