‹ BackHN Continuity

Thread

Anecdotally, programmers dislike "reduce"

175 points · 283 comments · vinhnx

  1. chubot · · focus · HN ↗
    Related to the point about worse performance, I'm pretty sure I was there when reduce was "banished" from Python 3 -- demoted to functools.reduce(), instead of the builtin reduce() in Python 2

    The story is that sometime in 2006 or 2007, Guido van Rossum was debugging why a web page in Google's internal code review tool (which he wrote) was taking 30+ seconds to render.

    This is basically a "production" incident, since thousands of Google engineers relied on the tool. Requests like this were probably tying up threads and exhausting thread pools, perhaps

    Eventually it was tracked down to a line wrapping algorithm written with reduce(). I don't think he wrote it -- it may have come in through a dependency. As many know, reduce() is basically:

         s1 + s2
         s1 + s2 + s3
         s1 + s2 + s3 + s4 
         ...
    
    And that's O(n^2) when s_i are strings. And I think it showed up if you viewed a 5000+ line diff, or a 5000+ line file. (Newer programs like Github also suffer here)

    I believe, in Python at that time, += was already optimized to avoid this (just like essentially all JS VMs are). Or you can use the idiom of append() to list and join() after.

    But reduce() basically forces the inefficient implementation, and I'm sure this is still true in Python 3.

    ---

    So basically Guido spent a long time debugging a performance problem related to reduce(), and made the decision to eject it, to help users avoid "footguns". I was his officemate at the time, so I recall this, but I wasn't involved directly

    Also, somebody contributed reduce() to Python way back in the 90's, as well as other functional idioms. He wouldn't have added that himself -- it was never his preferred style.

    He preferred a more imperative style. But he allowed those contributions, and then slightly regretted it later.

    <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.reduce" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.r...

    1. TZubiri · · focus · HN ↗
      The way I understand it, the map,filter,reduce functions in python exist as pythonic language constructs:

      -map: [x*2 for x in xs]

      -filter: [x for x in xs if x%0==2]

      -reduce: ummm..

      Maybe something like:

      sum = x+ret for x in xs from ret=0

      1. anitil · · focus · HN ↗
        Maybe the closest is &#x27;join&#x27; similar to `&quot;,&quot;.join([...])`? If we could replace the string with an operator
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.