Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subexpressions

Reply
Thread Tools

subexpressions

 
 
Sergey Dorofeev
Guest
Posts: n/a
 
      06-01-2007
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?


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-01-2007
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

 
Reply With Quote
 
 
 
 
Sergey Dorofeev
Guest
Posts: n/a
 
      06-01-2007

"Peter Otten" <(E-Mail Removed)> wrote in message
news:f3ok60$vp7$03$(E-Mail Removed)-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.


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-01-2007
Sergey Dorofeev wrote:

> "Peter Otten" <(E-Mail Removed)> wrote in message
> news:f3ok60$vp7$03$(E-Mail Removed)-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
 
Reply With Quote
 
Sergey Dorofeev
Guest
Posts: n/a
 
      06-01-2007

"Peter Otten" <(E-Mail Removed)> wrote in message
news:f3oo0p$c7c$03$(E-Mail Removed)-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.


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-01-2007
Sergey Dorofeev wrote:

>
> "Peter Otten" <(E-Mail Removed)> wrote in message
> news:f3oo0p$c7c$03$(E-Mail Removed)-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
 
Reply With Quote
 
Sergey Dorofeev
Guest
Posts: n/a
 
      06-01-2007

"Peter Otten" <(E-Mail Removed)> wrote in message
news:f3oret$sit$03$(E-Mail Removed)-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.


 
Reply With Quote
 
A.T.Hofkamp
Guest
Posts: n/a
 
      06-01-2007
On 2007-06-01, Sergey Dorofeev <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      06-01-2007
Sergey Dorofeev wrote:
> "Peter Otten" <(E-Mail Removed)> wrote in message
> news:f3ok60$vp7$03$(E-Mail Removed)-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.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      06-01-2007
Sergey Dorofeev wrote:
> "Peter Otten" <(E-Mail Removed)> wrote in message
> news:f3oo0p$c7c$03$(E-Mail Removed)-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.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sequence points in subexpressions amit.codename13@gmail.com C Programming 114 01-12-2010 10:01 PM
Re: subexpressions (OT: math) Steve Howell Python 23 06-04-2007 11:49 AM
Re: subexpressions Steve Howell Python 2 06-02-2007 02:39 PM
Examples of using "reluctant" subexpressions in regexps? david.karr@wamu.net Java 4 04-27-2005 07:46 PM
pre_match and post_match for subexpressions trans. (T. Onoma) Ruby 0 10-24-2004 03:41 AM



Advertisments