subexpressions

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

    #1

    subexpressions

    Hello all!

    Please help, is there way to use sub-expressions in lambda?
    For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
    lambda x: sin(x*x)+cos(x* x)
    How to make x*x to be evaluated once?


  • Peter Otten

    #2
    Re: subexpressions

    Sergey Dorofeev wrote:
    Please help, is there way to use sub-expressions in lambda?
    For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
    lambda x: sin(x*x)+cos(x* x)
    How to make x*x to be evaluated once?
    >>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +
    cos(.5*.5)
    True

    The real answer is of course: Use a function.

    Peter

    Comment

    • Sergey Dorofeev

      #3
      Re: subexpressions


      "Peter Otten" <__peter__@web. dewrote in message
      news:f3ok60$vp7 $03$1@news.t-online.com...
      Sergey Dorofeev wrote:
      >
      >Please help, is there way to use sub-expressions in lambda?
      >For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
      >lambda x: sin(x*x)+cos(x* x)
      >How to make x*x to be evaluated once?
      >
      >>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +
      cos(.5*.5)
      True
      >
      The real answer is of course: Use a function.
      But what about something like

      lambda x: sin(y)+cos(y) where y=x*x

      ?
      May be this could be a PEP? If there is no straight way to do this.


      Comment

      • Peter Otten

        #4
        Re: subexpressions

        Sergey Dorofeev wrote:
        "Peter Otten" <__peter__@web. dewrote in message
        news:f3ok60$vp7 $03$1@news.t-online.com...
        >Sergey Dorofeev wrote:
        >>
        >>Please help, is there way to use sub-expressions in lambda?
        >>For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
        >>lambda x: sin(x*x)+cos(x* x)
        >>How to make x*x to be evaluated once?
        >>
        >>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +
        >cos(.5*.5)
        >True
        >>
        >The real answer is of course: Use a function.
        >
        But what about something like
        >
        lambda x: sin(y)+cos(y) where y=x*x
        >
        ?
        May be this could be a PEP? If there is no straight way to do this.
        def f(x):
        y = x*x
        return sin(y) + cos(y)

        What is not straightforward about that?

        Peter

        Comment

        • Sergey Dorofeev

          #5
          Re: subexpressions


          "Peter Otten" <__peter__@web. dewrote in message
          news:f3oo0p$c7c $03$1@news.t-online.com...
          >>>Please help, is there way to use sub-expressions in lambda?
          >>>For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
          >>>lambda x: sin(x*x)+cos(x* x)
          >>>How to make x*x to be evaluated once?
          >>>
          >>>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5)
          >>>>>+
          >>cos(.5*.5)
          >>True
          >>>
          >>The real answer is of course: Use a function.
          >>
          >But what about something like
          >>
          >lambda x: sin(y)+cos(y) where y=x*x
          >>
          >?
          >May be this could be a PEP? If there is no straight way to do this.
          >
          def f(x):
          y = x*x
          return sin(y) + cos(y)
          >
          What is not straightforward about that?
          This code is needed once in a map, so I don't want 3+ extra lines.
          Solution seemed so simple...
          I always considered python as languague, where simple things do not require
          extensive coding.
          Moreover, this construction is common thing in functional programming.


          Comment

          • Peter Otten

            #6
            Re: subexpressions

            Sergey Dorofeev wrote:
            >
            "Peter Otten" <__peter__@web. dewrote in message
            news:f3oo0p$c7c $03$1@news.t-online.com...
            >>>>Please help, is there way to use sub-expressions in lambda?
            >>>>For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
            >>>>lambda x: sin(x*x)+cos(x* x)
            >>>>How to make x*x to be evaluated once?
            >>>>
            >>>>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5)
            >>>>>>+
            >>>cos(.5*.5)
            >>>True
            >>>>
            >>>The real answer is of course: Use a function.
            >>>
            >>But what about something like
            >>>
            >>lambda x: sin(y)+cos(y) where y=x*x
            >>>
            >>?
            >>May be this could be a PEP? If there is no straight way to do this.
            >>
            >def f(x):
            > y = x*x
            > return sin(y) + cos(y)
            >>
            >What is not straightforward about that?
            >
            This code is needed once in a map,
            Perhaps you like [sin(y)+cos(y) for y in (x*x for x in items)] then.
            so I don't want 3+ extra lines.
            What syntax would you suggest for a lambda enhanced to cover your use case?
            I suppose you will end up with roughly the same number of characters, all
            crammed in one line -- or broken into lines at a random position as it
            happens with overambitious list comprehensions.
            Solution seemed so simple...
            It /is/ simple.
            I always considered python as languague, where simple things do not
            require extensive coding.
            In Python, when conciseness and readability compete, readability tends to
            win (with the inline if...else as a notable exception).
            Moreover, this construction is common thing in functional programming.
            I can write Haskell in any language :-)

            Peter

            Comment

            • Sergey Dorofeev

              #7
              Re: subexpressions


              "Peter Otten" <__peter__@web. dewrote in message
              news:f3oret$sit $03$1@news.t-online.com...
              What syntax would you suggest for a lambda enhanced to cover your use
              case?
              I suppose you will end up with roughly the same number of characters, all
              crammed in one line -- or broken into lines at a random position as it
              happens with overambitious list comprehensions.
              Agree, this argument is strong.


              Comment

              • A.T.Hofkamp

                #8
                Re: subexpressions

                On 2007-06-01, Sergey Dorofeev <sergey@fidoman .ruwrote:
                Hello all!
                >
                Please help, is there way to use sub-expressions in lambda?
                For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
                lambda x: sin(x*x)+cos(x* x)
                How to make x*x to be evaluated once?
                lambda x: (lambda y: sin(y) + cos(y))(x*x)

                Albert

                Comment

                • Steve Holden

                  #9
                  Re: subexpressions

                  Sergey Dorofeev wrote:
                  "Peter Otten" <__peter__@web. dewrote in message
                  news:f3ok60$vp7 $03$1@news.t-online.com...
                  >Sergey Dorofeev wrote:
                  >>
                  >>Please help, is there way to use sub-expressions in lambda?
                  >>For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
                  >>lambda x: sin(x*x)+cos(x* x)
                  >>How to make x*x to be evaluated once?
                  >>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +
                  >cos(.5*.5)
                  >True
                  >>
                  >The real answer is of course: Use a function.
                  >
                  But what about something like
                  >
                  lambda x: sin(y)+cos(y) where y=x*x
                  >
                  ?
                  May be this could be a PEP? If there is no straight way to do this.
                  >
                  >
                  Or maybe it could be made a part of some other language. When
                  straightforward mechanisms (in rhis case, function definitins) exist to
                  avoid repeated computations it's very unlikely that such mangled
                  constructions will be made a part of Python.

                  If it *were* considered, you should at least change the "where" to
                  "for", and extend it to unpacking assignment to allow

                  lambda x, y: (sin(xx+yy) + cos(xx+yy) for xx, yy = x*x, y*y

                  regards
                  Steve
                  --
                  Steve Holden +1 571 484 6266 +1 800 494 3119
                  Holden Web LLC/Ltd http://www.holdenweb.com
                  Skype: holdenweb http://del.icio.us/steve.holden
                  ------------------ Asciimercial ---------------------
                  Get on the web: Blog, lens and tag your way to fame!!
                  holdenweb.blogs pot.com squidoo.com/pythonology
                  tagged items: del.icio.us/steve.holden/python
                  All these services currently offer free registration!
                  -------------- Thank You for Reading ----------------

                  Comment

                  • Steve Holden

                    #10
                    Re: subexpressions

                    Sergey Dorofeev wrote:
                    "Peter Otten" <__peter__@web. dewrote in message
                    news:f3oo0p$c7c $03$1@news.t-online.com...
                    >>>>Please help, is there way to use sub-expressions in lambda?
                    >>>>For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
                    >>>>lambda x: sin(x*x)+cos(x* x)
                    >>>>How to make x*x to be evaluated once?
                    >>>>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5)
                    >>>>>>+
                    >>>cos(.5*.5)
                    >>>True
                    >>>>
                    >>>The real answer is of course: Use a function.
                    >>But what about something like
                    >>>
                    >>lambda x: sin(y)+cos(y) where y=x*x
                    >>>
                    >>?
                    >>May be this could be a PEP? If there is no straight way to do this.
                    >def f(x):
                    > y = x*x
                    > return sin(y) + cos(y)
                    >>
                    >What is not straightforward about that?
                    >
                    This code is needed once in a map, so I don't want 3+ extra lines.
                    Solution seemed so simple...
                    I always considered python as languague, where simple things do not require
                    extensive coding.
                    Moreover, this construction is common thing in functional programming.
                    >
                    >
                    Stop thinking of three lines as "extensive coding" and your problem
                    disappears immediately.

                    regards
                    Steve
                    --
                    Steve Holden +1 571 484 6266 +1 800 494 3119
                    Holden Web LLC/Ltd http://www.holdenweb.com
                    Skype: holdenweb http://del.icio.us/steve.holden
                    ------------------ Asciimercial ---------------------
                    Get on the web: Blog, lens and tag your way to fame!!
                    holdenweb.blogs pot.com squidoo.com/pythonology
                    tagged items: del.icio.us/steve.holden/python
                    All these services currently offer free registration!
                    -------------- Thank You for Reading ----------------

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: subexpressions

                      Steve Holden a écrit :
                      (snip)
                      Stop thinking of three lines as "extensive coding" and your problem
                      disappears immediately.
                      Lol !
                      +1 QOTW

                      Comment

                      • Paul Boddie

                        #12
                        Re: subexpressions

                        On 1 Jun, 12:55, Steve Howell <showel...@yaho o.comwrote:
                        >
                        FWIW there's the possibility that even without a
                        subexpression syntax, some Python implementations
                        would detect the duplication of x*x and optimize that
                        for you. It would have to know that x*x had no side
                        effects, which I think is a safe assumption even in a
                        dynamic language like Python.
                        On the basis of you believing that x is one of the built-in numeric
                        types, yes, but how does the compiler know that?

                        Paul

                        Comment

                        • Terry Reedy

                          #13
                          Re: subexpressions


                          "Sergey Dorofeev" <sergey@fidoman .ruwrote in message
                          news:f3oj68$1pb 8$1@news.rtcomm .ru...
                          | How to make x*x to be evaluated once?

                          Addendum to the answers already posted: In Python,

                          lambda params: expression

                          is an inline abbreviation for

                          def <lambda>(params ): return expression

                          except that there is no external binding of the otherwise illegal
                          ..func_name '<lambda>'. The resulting function objects are otherwise
                          identical.

                          After years of discussion, Guido has decided to leave lambda alone for 3.0.
                          It will not be neither expanded, nor removed, nor renamed.

                          Terry Jan Reedy



                          Comment

                          • Kay Schluehr

                            #14
                            Re: subexpressions

                            On Jun 1, 9:51 am, "Sergey Dorofeev" <ser...@fidoman .ruwrote:
                            Hello all!
                            >
                            Please help, is there way to use sub-expressions in lambda?
                            For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
                            lambda x: sin(x*x)+cos(x* x)
                            How to make x*x to be evaluated once?
                            lambda x: (lambda y=x*x: math.sin(y)+mat h.cos(y))()

                            Kay

                            Comment

                            • Steven Bethard

                              #15
                              Re: subexpressions

                              Sergey Dorofeev wrote:
                              Please help, is there way to use sub-expressions in lambda?
                              For example, if I want to calculate sin(x^2)+cos(x^ 2) I must code:
                              lambda x: sin(x*x)+cos(x* x)
                              [and later]
                              This code is needed once in a map,
                              Peter Otten wrote:
                              Perhaps you like [sin(y)+cos(y) for y in (x*x for x in items)] then.
                              Just wanted to emphasize this suggestion so that it doesn't get lost in
                              the flood of lambda recommendations . If your code really looks like::

                              map(lambda x: sin(x * x) + cos(x * x), items)

                              you should be using a list comprehension instead. Using map() here is
                              not only more obscure and more verbose, but slower than::

                              [sin(x * x) + cos(x * x) for x in items]

                              From there, it's a simple nested generator comprehension to pull out
                              the subexpression:

                              [sin(y) + cos(y) for y in (x * x for x in items)]

                              If you aren't yet familiar with list and generator comprehensions, you
                              should take a few minutes to look at some of your uses of map() and
                              filter and see if you can simplify them using comprehensions instead.

                              STeVe

                              Comment

                              Working...