Replace reduce with listcomprehension?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ssecorp

    #1

    Replace reduce with listcomprehension?

    GvR wants to eliminate all the functional stuff (lambda map reduce
    filter) which kind of makes sense for Python, listcomprehensi ons are
    more efficient(at least as implemented inpython) from what i have
    gathered and they can express everything map/reduce/filter with
    crippled lambdas can.

    but what about this:
    reduce(lambda x,y:x*y+x,[1,2,3,4,5,6])

    how would I express that in a listcomprehensi on?
  • =?ISO-8859-1?Q?Luis_M=2E_Gonz=E1lez?=

    #2
    Re: Replace reduce with listcomprehensi on?

    Actually, GvR announced that "lambda, filter and map will stay (the
    latter two with small changes, returning iterators instead of lists).
    Only reduce will be removed from the 3.0 standard library. You can
    import it from functools."

    Check the full article here: http://www.artima.com/weblogs/viewpost.jsp?thread=98196

    Luis


    On Aug 25, 7:08 pm, ssecorp <circularf...@g mail.comwrote:
    GvR wants to eliminate all the functional stuff (lambda map reduce
    filter) which kind of makes sense for Python, listcomprehensi ons are
    more efficient(at least as implemented inpython) from what i have
    gathered and they can express everything map/reduce/filter with
    crippled lambdas can.
    >
    but what about this:
    reduce(lambda x,y:x*y+x,[1,2,3,4,5,6])
    >
    how would I express that in a listcomprehensi on?

    Comment

    • Diez B. Roggisch

      #3
      Re: Replace reduce with listcomprehensi on?

      ssecorp schrieb:
      GvR wants to eliminate all the functional stuff (lambda map reduce
      filter) which kind of makes sense for Python, listcomprehensi ons are
      more efficient(at least as implemented inpython) from what i have
      gathered and they can express everything map/reduce/filter with
      crippled lambdas can.
      >
      but what about this:
      reduce(lambda x,y:x*y+x,[1,2,3,4,5,6])
      >
      how would I express that in a listcomprehensi on?
      You can't. And AFAIK your are wrong - GvR doesn't wan to eliminate the
      functional stuff.


      Some ideas are just bad. While some thoughts on Python evolution are constructive, some go against the basic tenets of Python so egregiously that it would be like asking someone to run in a circle: it gets you nowhere, even for Python 3000, where extra...




      Diez

      Comment

      • =?ISO-8859-1?Q?Luis_M=2E_Gonz=E1lez?=

        #4
        Re: Replace reduce with listcomprehensi on?

        Correction:
        I guess the link I provided above is old news...
        In the more recent Python 3000 FAQ, GvR says:

        Q. If you're killing reduce(), why are you keeping map() and filter()?

        A. I'm not killing reduce() because I hate functional programming; I'm
        killing it because almost all code using reduce() is less readable
        than the same thing written out using a for loop and an accumulator
        variable. On the other hand, map() and filter() are often useful and
        when used with a pre-existing function (e.g. a built-in) they are
        clearer than a list comprehension or generator expression. (Don't use
        these with a lambda though; then a list comprehension is clearer and
        faster.)

        So it seems only reduce will be eliminated.

        Luis

        Comment

        • Diez B. Roggisch

          #5
          Re: Replace reduce with listcomprehensi on?

          Luis M. González schrieb:
          Correction:
          I guess the link I provided above is old news...
          In the more recent Python 3000 FAQ, GvR says:
          >
          Q. If you're killing reduce(), why are you keeping map() and filter()?
          >
          A. I'm not killing reduce() because I hate functional programming; I'm
          killing it because almost all code using reduce() is less readable
          than the same thing written out using a for loop and an accumulator
          variable. On the other hand, map() and filter() are often useful and
          when used with a pre-existing function (e.g. a built-in) they are
          clearer than a list comprehension or generator expression. (Don't use
          these with a lambda though; then a list comprehension is clearer and
          faster.)
          >
          So it seems only reduce will be eliminated.
          Nope. From the link you provided yourself:

          """
          Only reduce will be removed from the 3.0 standard library. You can
          import it from functools.
          """

          Diez

          Comment

          • bearophileHUGS@lycos.com

            #6
            Re: Replace reduce with listcomprehensi on?

            Luis M. González, citing Guido:
            A. I'm not killing reduce() because I hate functional programming; I'm
            killing it because almost all code using reduce() is less readable
            than the same thing written out using a for loop and an accumulator
            variable.
            (So reduce is now in itertools). Try to pry folds from the hands of
            haskell programmers :-)
            I think they think that folds are more clear than explicit loops...

            Bye,
            bearophile

            Comment

            • Steven D'Aprano

              #7
              Re: Replace reduce with listcomprehensi on?

              On Tue, 26 Aug 2008 00:53:35 +0200, Diez B. Roggisch wrote:
              >So it seems only reduce will be eliminated.
              >
              Nope. From the link you provided yourself:
              >
              """
              Only reduce will be removed from the 3.0 standard library. You can
              import it from functools.
              """
              functools isn't in the standard library???

              *wink*

              Seriously, I think Guido meant "built-ins". Since I don't agree with him
              that reduce() is hard to read, I disagree that accumulators are easier to
              use. But since reduce is only an import away, I'm satisfied.


              --
              Steven

              Comment

              Working...