map/filter/reduce/lambda opinions and background unscientificmini-survey

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

    #16
    Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

    I have never written a line of Lisp or Scheme, so it took me a while to
    grok "lambda" as a synonym for "expression to be evaluated later". I
    then thought it was similar to Smalltalk's functional closures, since
    you can define local arguments at the beginning of the block, and then
    write the body of the block using those args. But then I saw that it
    was only a subset of that capability, since one may only implement an
    expression in a lambda, not a full code block.

    Even with those limitations, I've found lambda to be a nice compact
    form for specifying callbacks. It is especially helpful in pyparsing,
    where I define a mechanism for the programmer to specify a parse action
    to be performed, which can modify the matched tokens. Here is one that
    is very compact:

    quotedString.se tParseResults( lambda s,loc,toks: toks[0][1:-1] )

    Parse actions take 3 arguments: the original string being parsed, the
    location of the beginning of the match, and a ParseResults object
    containing the matched tokens (ParseResults objects can act as a list,
    dict, or object with attributes). The purpose of this parse action is
    to remove the opening and closing quotation marks from the matched
    quoted string. One of the things I especially like about this
    simplicity of parse actions is that there is no need for checking
    whether toks is an empty list, or if the first and last characters are
    quotation marks before stripping them - quotedStrings call their parse
    actions *only* with a single element list, and only with the first
    element containing a string with opening and closing quotes. Still, in
    anticipation of the demise of lambda, and because this function is
    frequently needed, I've added it as a built-in helper function to
    pyparsing, called removeQuotes. But lambda is very simple and
    immediate for defining such simple transforms, and there is no need to
    go track down where a named function definition may be found. (Yes, I
    *could* stop in my tracks just prior to this statement and define this
    one-line function, but then this interrupts the flow of my grammar
    definition.)

    It seems to me that lambda's built-in limitation of *only* supporting
    expressions, rather than complete code blocks, has led to people
    applying their boundless creativity to trying to cram conditional logic
    into bewildering and failure-prone boolean expressions, and it reminds
    me of some of the C macro coding that I had to sift through about 20
    years ago.

    Not coincidentally, I think the lambda limitation is the true origin of
    may of the requests we read on c.l.py for the proper syntax for
    Python's version of the ternary ?: operator as in "how do I write
    (x>10? a : b) in Python", which is invariably followed by a post such
    as, "don't bother with that, just do "(x>10 and a or b)", which is then
    usually followed with, "but watch out in case a evaluates to False..."

    So personally, I like lambdas even if I am not found of the keyword
    "lambda". Maybe we could replace "lambda" with "@" or "$"?

    -- Paul

    Comment

    • Sean McIlroy

      #17
      Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

      Peter Hansen wrote:
      <snip>[color=blue]
      > Sean, what gave you the impression this would change?[/color]

      just inductive reasoning. i've been wrong before (like anyone who makes
      that claim), and i'm a former python enthusiast, so my judgement must
      be colored to some extent by bitterness. maybe they have solid reasons
      for scrapping the functional constructs. but to me it seems like
      they're eliminating them just because they offend the sensibilities of
      C-programmers. (i mean those stereotypical C-programmers, baffled by
      recursion and the like, who don't want to be reproached with the fact
      of their mathematical illiteracy.) if that's the case then list
      comprehensions and/or "first class functions" are likely to be the next
      target. even if they're not, it's pretty clear that python is leaving
      its multiparadigmat ic origins behind. "do it our way," the pundits are
      effectively saying, "or get out". for my part, i'm getting out.

      Comment

      • Chris Smith

        #18
        Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

        >>>>> "Devan" == Devan L <devlai@gmail.c om> writes:

        Devan> None of them are really indispensible. Map and filter cab
        Devan> be replaced with list comprehensions. reduce is redundant
        Devan> except when multiplying a series; there's a sum function
        Devan> for a reason. Lambda looks cleaner in some cases, but you
        Devan> don't gain any functionality.

        Devan> What really struck me, though, is the last line of the
        Devan> abstract:

        Devan> "I expect tons of disagreement in the feedback, all from
        Devan> ex-Lisp-or-Scheme folks. :-)"

        Devan> Guido wrote somewhere that the original map, filter, and
        Devan> reduce came from a lisp hacker who missed them.

        My question is, why not move them into, say, a "functional " library,
        so that legacy code can be handled via an import, and those heads
        preferring to think that way can be satisfied, and those little corner
        cases not handled by the newer, sweller syntaxes can still be managed?
        IOW, just ripping them out of the core and leaving everyone in the
        lurch doesn't seem too pythonic to me.
        Best,
        Chris

        Comment

        • Erik Max Francis

          #19
          Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

          Sean McIlroy wrote:
          [color=blue]
          > if that's the case then list
          > comprehensions and/or "first class functions" are likely to be the next
          > target.[/color]

          Slippery slope arguments are logical fallacies, you know.

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          Can I walk with you / Through your life
          -- India Arie

          Comment

          • Robert Kern

            #20
            Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

            Chris Smith wrote:[color=blue]
            > My question is, why not move them into, say, a "functional " library,
            > so that legacy code can be handled via an import, and those heads
            > preferring to think that way can be satisfied, and those little corner
            > cases not handled by the newer, sweller syntaxes can still be managed?
            > IOW, just ripping them out of the core and leaving everyone in the
            > lurch doesn't seem too pythonic to me.[/color]

            Python 3000 will be breaking more backwards compatibility than
            map/reduce/filter. That legacy code won't work anyways.

            I predict, though, that one of the first 3rd party modules to come out
            for Python 3000 will be such a library.

            --
            Robert Kern
            rkern@ucsd.edu

            "In the fields of hell where the grass grows high
            Are the graves of dreams allowed to die."
            -- Richard Harter

            Comment

            • Robert Kern

              #21
              Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

              Sean McIlroy wrote:[color=blue]
              > Peter Hansen wrote:
              > <snip>
              >[color=green]
              >>Sean, what gave you the impression this would change?[/color]
              >
              > just inductive reasoning. i've been wrong before (like anyone who makes
              > that claim), and i'm a former python enthusiast, so my judgement must
              > be colored to some extent by bitterness. maybe they have solid reasons
              > for scrapping the functional constructs. but to me it seems like
              > they're eliminating them just because they offend the sensibilities of
              > C-programmers.[/color]

              This is incorrect.
              [color=blue]
              > (i mean those stereotypical C-programmers, baffled by
              > recursion and the like, who don't want to be reproached with the fact
              > of their mathematical illiteracy.) if that's the case then list
              > comprehensions and/or "first class functions" are likely to be the next
              > target.[/color]

              map and filter are being removed *because of* list comprehensions. Did
              you even read Guido's articles about this issue? Your understanding of
              why these changes are planned is incorrect; consequently your projection
              based on that understanding is not on firm footing.
              [color=blue]
              > even if they're not, it's pretty clear that python is leaving
              > its multiparadigmat ic origins behind. "do it our way," the pundits are
              > effectively saying, "or get out". for my part, i'm getting out.[/color]

              If that's what you want to do, no one is going to stop you. But please
              do it quietly.

              --
              Robert Kern
              rkern@ucsd.edu

              "In the fields of hell where the grass grows high
              Are the graves of dreams allowed to die."
              -- Richard Harter

              Comment

              • Mike Meyer

                #22
                Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                "Sean McIlroy" <sean_mcilroy@y ahoo.com> writes:
                [color=blue]
                > Peter Hansen wrote:
                > <snip>[color=green]
                >> Sean, what gave you the impression this would change?[/color]
                > if that's the case then list comprehensions and/or "first class
                > functions" are likely to be the next target.[/color]

                The existence of list comprehensions are the reason that these
                functions are going away, so they aren't likely to be next. It's all
                part of "There should be one-- and preferably only one --obvious way
                to do it."

                Personally, I hope they wind up in a "functional " module, so you can add
                "from functional import *" to the top of your scripts, and keep doing
                exactly what you've been doing.

                <mike
                --
                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                Comment

                • Ron Adam

                  #23
                  Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                  Terry Hancock wrote:
                  [color=blue]
                  > On Friday 01 July 2005 03:36 pm, Ron Adam wrote:
                  >[color=green]
                  >>I find map too limiting, so won't miss it. I'm +0 on removing lambda
                  >>only because I'm unsure that there's always a better alternative.[/color]
                  >
                  >
                  > Seems like some new idioms would have to be coined, like:
                  >
                  > def my_function(a1, a2):
                  > def _(a,b): return a+b
                  > call_a_lib_w_ca llback(callback =_)
                  >
                  > doesn't seem too bad, and defeats the "wasting time thinking of names"
                  > argument.[/color]

                  Usually the time is regained later when you need to go back and figure
                  out what it is that the lambda is doing. Not so easy for beginners.

                  A hard to understand process that is easy to do, is not easier than an
                  easy to understand process that is a bit harder to do.

                  [color=blue][color=green]
                  >>So what would be a good example of a lambda that couldn't be replaced?[/color]
                  >
                  > I suspect the hardest would be building a list of functions. Something
                  > like:
                  >
                  > powers = [lambda a, i=i: a**i for i in range(10)]
                  >
                  > which you might be able to make like this:
                  >
                  > powers = []
                  > for i in range(10):
                  > def _(a,i=i): return a**i
                  > powers.append(_ )
                  >
                  > which works and is understandable, but a bit less concise.[/color]

                  This would be a more direct translation I think:

                  def put_func(i):
                  def power_of_i(a):
                  return a**i
                  return power_of_i
                  power = [put_func(i) for i in range(10)]

                  I think it's also clearer what it does. I had to type the lambda
                  version into the shell to be sure I understood it. I think that's what
                  we want to avoid.
                  [color=blue]
                  > The main obstacle to the lambda style here is that def statements
                  > are not expressions. I think that's been proposed as an alternative,
                  > too -- make def return a value so you could say:
                  >
                  > powers = [def _(a,i=i): return a**i for i in range(10)][/color]

                  Wouldn't it be:

                  powers = [(def _(a): return a**i) for i in range(10)]

                  The parens around the def make it clearer I think.

                  That would be pretty much just renaming lambda and changing a syntax a
                  tad. I get the feeling that the continual desire to change the syntax
                  of lambda is one of the reasons for getting rid of it. I'm not sure any
                  of the suggestions will fix that. Although I prefer this version over
                  the current lambda.

                  Instead of reusing 'def', resurrecting 'let' as a keyword might be an
                  option.

                  powers = [ (let a return a**i) for i in range(10) ]


                  I just now thought this up, but I like it a lot as an alternative syntax
                  to lambda. :-)

                  [color=blue]
                  > Personally, I think this is understandable, and given that lambda
                  > is to be pulled, a nice substitute (I would say it is easier to read
                  > than the current lambda syntax, and easier for a newbie to
                  > understand).
                  >
                  > But it would probably encourage some bad habits, such as:
                  >
                  > myfunc = def _(a,b):
                  > print a,b
                  > return a+b
                  >
                  > which looks too much like Javascript, to me, where there are
                  > about three different common idioms for defining a
                  > function (IIRC). :-/[/color]

                  I don't think it would be used that way... Very often anyway.

                  Still none of these examples make for good use cases for keeping lambda.
                  And I think that's whats needed first, then the new syntax can be decided.

                  Ron

                  Comment

                  • Dennis Lee Bieber

                    #24
                    Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

                    On Fri, 01 Jul 2005 19:24:09 -0700, Robert Kern <rkern@ucsd.edu >
                    declaimed the following in comp.lang.pytho n:
                    [color=blue]
                    > I predict, though, that one of the first 3rd party modules to come out
                    > for Python 3000 will be such a library.[/color]

                    Should be a breeze, considering the work that had to go into
                    making a ComeFrom module...

                    --[color=blue]
                    > =============== =============== =============== =============== == <
                    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                    > wulfraed@dm.net | Bestiaria Support Staff <
                    > =============== =============== =============== =============== == <
                    > Home Page: <http://www.dm.net/~wulfraed/> <
                    > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                    Comment

                    • Donn Cave

                      #25
                      Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

                      Quoth Tom Anderson <twic@urchin.ea rth.li>:
                      ....
                      | I disagree strongly with Guido's proposals, and i am not an ex-Lisp,
                      | -Scheme or -any-other-functional-language programmer; my only other real
                      | language is Java. I wonder if i'm an outlier.
                      |
                      | So, if you're a pythonista who loves map and lambda, and disagrees with
                      | Guido, what's your background? Functional or not?

                      Dysfunctional, I reckon.

                      I think I disagree with the question more than the answer.

                      First, map and lambda are two different things, and it's reasonable
                      to approve of one and abhor the other. Especially if you have a
                      background in a functional language where lambda works like it should.
                      On the other hand, the list comprehension gimmick that replaces some
                      of the "higher order functions" is borrowed from Haskell, as you probably
                      know, so it isn't exactly alien to functional programming. Prelude.hs
                      defines map: map f xs = [ f x | x <- xs ]

                      Secondly, if there's anything I detest about the Python development
                      model, it is the tendency to focus on gimmicks. For 2.X, elimination
                      of these features would be an atrocity, a gratuitous change that would
                      break programs - but I don't think anyone who counts has seriously
                      proposed to do that. With 3.X, we are talking about a different language.
                      May not ever even get off the ground, but if it does, it's supposed to
                      be distinctly different, and we need to know a lot more about it before
                      we can reasonably worry about trivial details like whether map is going
                      to be there.

                      I personally think real FP is seriously hot stuff, but I think Python
                      is a lousy way to do it, with or without map. I suppose there's a
                      remote possibility that 3.X will change all that. Or more likely,
                      there will by then be a really attractive FP language, maybe out
                      of the "links" initiative by Wadler et al.

                      Donn Cave, donn@drizzle.co m

                      Comment

                      • John Roth

                        #26
                        Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                        "Robert Kern" <rkern@ucsd.edu > wrote in message
                        news:mailman.12 26.1120271406.1 0512.python-list@python.org ...
                        [color=blue]
                        >
                        > map and filter are being removed *because of* list comprehensions. Did you
                        > even read Guido's articles about this issue? Your understanding of why
                        > these changes are planned is incorrect; consequently your projection based
                        > on that understanding is not on firm footing.[/color]

                        May I most respectfully point out that you've got it backwards.
                        Part of the justification for list comprehensions was that they could
                        be used to replace map and filter.

                        The jihad against the functional constructs has been going on for a
                        long time, and list comprehensions are only one piece of it.

                        John Roth
                        [color=blue]
                        > --
                        > Robert Kern
                        > rkern@ucsd.edu
                        >
                        > "In the fields of hell where the grass grows high
                        > Are the graves of dreams allowed to die."
                        > -- Richard Harter
                        >[/color]

                        Comment

                        • Tom Anderson

                          #27
                          Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                          On Fri, 1 Jul 2005, Ivan Van Laningham wrote:
                          [color=blue]
                          > Personally, I find that Lisp & its derivatives put your head in a very
                          > weird place. Even weirder than PostScript/Forth/RPN, when you come
                          > right down to it.[/color]

                          +1 QOTW!

                          tom

                          --
                          REMOVE AND DESTROY

                          Comment

                          • Robert Kern

                            #28
                            Re: map/filter/reduce/lambda opinions and backgroundunsci entificmini-survey

                            John Roth wrote:[color=blue]
                            > "Robert Kern" <rkern@ucsd.edu > wrote in message
                            > news:mailman.12 26.1120271406.1 0512.python-list@python.org ...
                            >[color=green]
                            >>map and filter are being removed *because of* list comprehensions. Did you
                            >>even read Guido's articles about this issue? Your understanding of why
                            >>these changes are planned is incorrect; consequently your projection based
                            >>on that understanding is not on firm footing.[/color]
                            >
                            > May I most respectfully point out that you've got it backwards.
                            > Part of the justification for list comprehensions was that they could
                            > be used to replace map and filter.[/color]

                            That is essentially what I said. List comprehensions were made to
                            replace map and filter. Now that they are here, Guido wants to remove
                            map and filter to keep OOWTDI.

                            My response was incomplete, not incorrect.

                            --
                            Robert Kern
                            rkern@ucsd.edu

                            "In the fields of hell where the grass grows high
                            Are the graves of dreams allowed to die."
                            -- Richard Harter

                            Comment

                            • Jamey Cribbs

                              #29
                              Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                              Tom Anderson wrote:[color=blue]
                              > So, if you're a pythonista who loves map and lambda, and disagrees with
                              > Guido, what's your background? Functional or not?[/color]

                              I have no functional language background. Until recently, I had no use
                              for programming "expression to be evaluated later" or "deferred
                              expressions" or whatever else they are being called.

                              Where I came to see the awesomeness of "deferred expressions" was a few
                              months ago when I started a major rewrite of KirbyBase for Ruby. I
                              wanted to make the Ruby version of KirbyBase take advantage of the
                              strengths of the language. Another Ruby programmer, Hal Fulton, was
                              helping me by constantly pushing me to make KirbyBase more Ruby-ish.
                              One thing he kept pushing was to be able to specify select querys using
                              Ruby's "deferred expression" mechanism, code blocks (before anyone
                              starts yelling, I know that Ruby code blocks are *much* more than just
                              "deferred expressions"; I'm just using that descriptor here for the sake
                              of this discussion).

                              Code blocks allow you to wrap up any Ruby code and pass it to a method
                              and have it executed within that method. It is more powerful than
                              lambda, because you can have multiple statements in the code block and
                              you can do assignment within the code block. This allowed me to rewrite
                              KirbyBase so that you can do a select like this:

                              plane_tbl.selec t { |r| r.country == 'USA' and r.speed > 350 }

                              Now, this is cool, but you can do this using lambda in Python. Where
                              Ruby code blocks really shine is that you can also do this:

                              plane_tbl.updat e {|r| r.name == 'P-51'}.set {|r|
                              r.speed = 405
                              r.range = 1210
                              }

                              I have one code block that I pass to the update method which says
                              "Select all planes with name equal to P-51". Then, I pass a code block
                              to the set method which assigns new values to the speed and range fields
                              for those records (i.e. P-51) that were selected in the update method.
                              This is something you can't do with lambda.

                              Now, I think I can duplicate the same functionality of Ruby code blocks
                              by using Python functions, but it is not going to be as pretty.

                              So, even though lambda is not as powerful as Ruby code blocks, I was
                              still bummed to read that it is going away, because it is better than
                              nothing.

                              Hopefully, Guido will reconsider or, even better, give us something even
                              more powerful.

                              Jamey Cribbs

                              Comment

                              • Mike Meyer

                                #30
                                Re: map/filter/reduce/lambda opinions and background unscientificmin i-survey

                                Jamey Cribbs <jcribbs@twmi.r r.com> writes:[color=blue]
                                > Code blocks allow you to wrap up any Ruby code and pass it to a method
                                > and have it executed within that method. It is more powerful than
                                > lambda, because you can have multiple statements in the code block and
                                > you can do assignment within the code block.[/color]

                                Just FYI, the inability to have statements - even multiple statements
                                - in a lambda is what people are talking about when they talk about
                                the limitations of lambda. It's a common thing for someone with a
                                background that includes a proper lambda to trip over when they first
                                start programming in Python. It's not that uncommon for newcommers to
                                trip over it with "print".

                                <mike
                                --
                                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                                Comment

                                Working...