‹ BackHN Continuity

Thread

Anecdotally, programmers dislike "reduce"

175 points · 283 comments · vinhnx

  1. rspeele · · focus · HN ↗
    Map and filter usually have only one arg and if they have 2, the 2nd is almost always a 0-based index. They look identical in most languages, even when Microsoft chooses to call them Select and Where.

    Reduce has an accumulator and a 2-arg function and languages are not very consistent amongst each other as to whether it's reduce(initial_acc, callback(acc, elem)) or reduce(callback(acc, elem), initial_acc) or reduce(callback(elem, acc), initial_acc) or what.

    Hard to remember. Also some languages have a version of reduce that doesn't take an initial accumulator at all, which is just a footgun waiting for you to hit an empty collection. Also ALSO, the accumulator can easily become awkward in languages that don't support anonymous types or don't support easy mutation of an anonymous type record. Which is most of them!

    1. jiehong · · focus · HN ↗
      > … to call them Select and Where.

      While map is a great name, I always struggle to remember if ‘filter’ keeps elements that match the condition or removes them.

      I mean, it’s like a colander: you filter noodles and water, but which one do you keep? The noodles, right? But, replace noodles with tea and now you want to keep the water part.

      Naming is hard I guess.

      1. dcminter · · focus · HN ↗
        If you're making tea with a colander something is very wrong ;)
        1. NooneAtAll3 · · focus · HN ↗
          depends on the size of the sieve, but sometimes one does cook a whole stewpot of tea at once (f.e. in canteen)
          1. dcminter · · focus · HN ↗
            A colander would allow the tea leaves through to no purpose. A colander is a type of sieve but a sieve is not necessarily a colander.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.