# subexpressions - Python

This is a discussion on subexpressions - Python ; 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?...

1. ## subexpressions

Hello all!

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?

2. ## Re: subexpressions

Sergey Dorofeev wrote:

> 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

3. ## Re: subexpressions

"Peter Otten" <__peter__@web.de> wrote in message
news:f3ok60\$vp7\$03\$1@news.t-online.com...
> Sergey Dorofeev wrote:
>
>> 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.

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.

4. ## Re: subexpressions

Sergey Dorofeev wrote:

> "Peter Otten" <__peter__@web.de> wrote in message
> news:f3ok60\$vp7\$03\$1@news.t-online.com...
>> Sergey Dorofeev wrote:
>>
>>> 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

5. ## Re: subexpressions

"Peter Otten" <__peter__@web.de> wrote in message
news:f3oo0p\$c7c\$03\$1@news.t-online.com...
>>>> 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.

6. ## Re: subexpressions

Sergey Dorofeev wrote:

>
> "Peter Otten" <__peter__@web.de> wrote in message
> news:f3oo0p\$c7c\$03\$1@news.t-online.com...
>>>>> 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.

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

7. ## Re: subexpressions

"Peter Otten" <__peter__@web.de> wrote 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.

8. ## Re: subexpressions

On 2007-06-01, Sergey Dorofeev <sergey@fidoman.ru> wrote:
> Hello all!
>
> 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

9. ## Re: subexpressions

Sergey Dorofeev wrote:
> "Peter Otten" <__peter__@web.de> wrote in message
> news:f3ok60\$vp7\$03\$1@news.t-online.com...
>> Sergey Dorofeev wrote:
>>
>>> 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 ----------------

10. ## Re: subexpressions

Sergey Dorofeev wrote:
> "Peter Otten" <__peter__@web.de> wrote in message
> news:f3oo0p\$c7c\$03\$1@news.t-online.com...
>>>>> 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 ----------------