explain this function to me, lambda confusion

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

    #16
    Re: explain this function to me, lambda confusion

    >
    Both the responses offer lambda free alternatives. That's fine, and
    given the terse documentation and problems that I had understanding
    them, I would agree. So what applications are lambdas suited to? I
    think the parameterised function model is one.
    What else?
    i've hardly ever used lambdas since map() and filter() were replaced by list
    comprehension. two other uses I can think of for it are: using it as a
    sorting key (which takes a function and lambdas are perfect for that when a
    direct function isn't available. for example, lambda x: x.myName), and I
    made an irc bot once that certain events had a list of fuctions that would
    be called after that event. it was like being able to dynamically add and
    remove event handlers. for example what if you asked the user a question
    and you wanted to know for the next input whether it was from that user and
    was an answer to that question. sometimes the function to add would be very
    simple, so writing a def for it would just be ugly.


    Comment

    • Terry Reedy

      #17
      Re: explain this function to me, lambda confusion


      "Arnaud Delobelle" <arnodel@google mail.comwrote in message
      news:m2ve1b7msb .fsf@googlemail .com...
      | Lie <Lie.1296@gmail .comwrites:
      |
      | Lambda can actually be safely removed from python and no other
      | features would be missing. It is always possible to create a def
      | version of any lambda, so lambda is useless. It is just a convenience
      | for the times where we're just too lazy to invent a name and find a
      | place to place the def, instead just inlining the function.
      |
      | Note that the same thing can be said about generator expressions,
      | which are nothing more than anonymous, non-reusable, generator
      | functions.

      Right. So if someone posted on genexp confusion, I would suggest
      'write a full generator function'.

      | Instead these were _added_ to the language!

      As a convenience.
      Actually, if one uses more that one for-clause in a generator expression,
      there is a potential gotcha in relation to name capture. So if that bites,
      the genexp is not so much a convenience, and one might better write
      the full function.

      tjr



      Comment

      • Arnaud Delobelle

        #18
        Re: explain this function to me, lambda confusion

        On May 19, 5:22 am, "Terry Reedy" <tjre...@udel.e duwrote:
        "Arnaud Delobelle" <arno...@google mail.comwrote in message
        [...]
        | Note that the same thing can be said about generator expressions,
        | which are nothing more than anonymous, non-reusable, generator
        | functions.
        >
        Right. So if someone posted on genexp confusion, I would suggest
        'write a full generator function'.
        I was just arguing against arguing for the removal of lambda on the
        basis that it doesn't add any functionality to the language!
        | Instead these were _added_ to the language!
        >
        As a convenience.
        Actually, if one uses more that one for-clause in a generator expression,
        there is a potential gotcha in relation to name capture. So if that bites,
        the genexp is not so much a convenience, and one might better write
        the full function.
        >
        tjr
        Yes, IMHO this is a bug, and I wish I had the time to dive into the
        code to see if I can fix it.

        --
        Arnaud

        Comment

        • Bruno Desthuilliers

          #19
          Re: explain this function to me, lambda confusion

          inhahe a écrit :
          >Both the responses offer lambda free alternatives. That's fine, and
          >given the terse documentation and problems that I had understanding
          >them, I would agree. So what applications are lambdas suited to? I
          >think the parameterised function model is one.
          >What else?
          >
          i've hardly ever used lambdas since map() and filter() were replaced by list
          comprehension. two other uses I can think of for it are: using it as a
          sorting key (which takes a function and lambdas are perfect for that when a
          direct function isn't available. for example, lambda x: x.myName),
          import operator
          foos.sort(key=o perator.attrget ter('myName'))


          Comment

          • Terry Reedy

            #20
            Re: explain this function to me, lambda confusion


            "Arnaud Delobelle" <arnodel@google mail.comwrote in message
            news:6062786d-6f83-4850-bf55-4db4ec27bdea@b1 g2000hsg.google groups.com...
            | On May 19, 5:22 am, "Terry Reedy" <tjre...@udel.e duwrote:
            | "Arnaud Delobelle" <arno...@google mail.comwrote in message
            | [...]
            | | Note that the same thing can be said about generator expressions,
            | | which are nothing more than anonymous, non-reusable, generator
            | | functions.
            | >
            | Right. So if someone posted on genexp confusion, I would suggest
            | 'write a full generator function'.
            |
            | I was just arguing against arguing for the removal of lambda on the
            | basis that it doesn't add any functionality to the language!

            I sort of understood that ;-)
            Like Guido, I am split on keep/remove.
            However, I have decided to leave lambda out of my Python-subset
            executable-pseudocode algorithm language. I have not decided whether or
            not to include genexps.

            | | Instead these were _added_ to the language!
            | >
            | As a convenience.
            | Actually, if one uses more that one for-clause in a generator
            expression,
            | there is a potential gotcha in relation to name capture. So if that
            bites,
            | the genexp is not so much a convenience, and one might better write
            | the full function.

            | Yes, IMHO this is a bug, and I wish I had the time to dive into the
            | code to see if I can fix it.

            If I do include them, I might restrict them to one for-clause because of
            that glitch, whose details I keep forgetting.

            tjr
            |



            Comment

            • Paul McGuire

              #21
              Re: explain this function to me, lambda confusion

              On May 18, 10:41 am, "inhahe" <inh...@gmail.c omwrote:
              Both the responses offer lambda free alternatives. That's fine, and
              given the terse documentation and problems that I had understanding
              them, I would agree. So what applications are lambdas suited to? I
              think the parameterised function model is one.
              What else?
              >
              i've hardly ever used lambdas since map() and filter() were replaced by list
              comprehension.  two other uses I can think of for it are: using it as a
              sorting key (which takes a function and lambdas are perfect for that when a
              direct function isn't available. for example, lambda x: x.myName), and I
              made an irc bot once that certain events had a list of fuctions that would
              be called after that event.  it was like being able to dynamically add and
              remove event handlers.  for example what if you asked the user a question
              and you wanted to know for the next input whether it was from that user and
              was an answer to that question.  sometimes the function to add would be very
              simple, so writing a def for it would just be ugly.
              lambda is handy in defining parse actions in pyparsing. Parse actions
              are callbacks to be run when an expression within a larger grammar is
              matched. A common use for parse actions is to do some sort of text or
              type conversion. The simplest parse actions are called using the list
              of matched tokens. Here is a subexpression that will convert numeric
              strings found in a larger grammar to ints:

              integer = Word("012345678 9").setParseAct ion(lambda tokens:
              int(tokens[0]) )

              Since this returns an actual int, there is no need to junk up the post-
              parsing code with calls to int(), float(), etc. for these simple
              conversions.

              Here is an example parse action that just converts a set of matched
              words to title case:

              title = OneOrMore(Word( alphas)).setPar seAction(lambda tokens: "
              ".join([ t.title() for t in tokens ]) )
              print title.parseStri ng("the sun also rises")[0]

              prints:
              The Sun Also Rises

              This second example is about as complex as I'd like to get in a
              lambda, though. Anything more elaborate than that, and I'd go with a
              separately defined function.

              -- Paul


              Comment

              • Arnaud Delobelle

                #22
                Re: explain this function to me, lambda confusion

                Paul McGuire <ptmcg@austin.r r.comwrites:

                [...]
                lambda is handy in defining parse actions in pyparsing. Parse actions
                are callbacks to be run when an expression within a larger grammar is
                matched. A common use for parse actions is to do some sort of text or
                type conversion. The simplest parse actions are called using the list
                of matched tokens. Here is a subexpression that will convert numeric
                strings found in a larger grammar to ints:
                >
                integer = Word("012345678 9").setParseAct ion(lambda tokens:
                int(tokens[0]) )
                Could you use it as a decoratore instead?

                integer = Word("012345678 9")

                @integer.setPar seAction
                def parse_integer(t okens):
                return int(tokens[0])

                I could make your grammar clearer, because you don't mix it with
                processing code... and no need for lambdas!

                --
                Arnaud

                Comment

                Working...