By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

subexpressions

P: n/a
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?
Jun 1 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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

Jun 1 '07 #2

P: n/a

"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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.
Jun 1 '07 #3

P: n/a
Sergey Dorofeev wrote:
"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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
Jun 1 '07 #4

P: n/a

"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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.
Jun 1 '07 #5

P: n/a
Sergey Dorofeev wrote:
>
"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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
Jun 1 '07 #6

P: n/a

"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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.
Jun 1 '07 #7

P: n/a
On 2007-06-01, Sergey Dorofeev <se****@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

Jun 1 '07 #8

P: n/a
Sergey Dorofeev wrote:
"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

Jun 1 '07 #9

P: n/a
Sergey Dorofeev wrote:
"Peter Otten" <__*******@web.dewrote in message
news:f3*************@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.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

Jun 1 '07 #10

P: n/a
Steve Holden a écrit :
(snip)
Stop thinking of three lines as "extensive coding" and your problem
disappears immediately.
Lol !
+1 QOTW
Jun 1 '07 #11

P: n/a
On 1 Jun, 12:55, Steve Howell <showel...@yahoo.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

Jun 1 '07 #12

P: n/a

"Sergey Dorofeev" <se****@fidoman.ruwrote in message
news:f3***********@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

Jun 1 '07 #13

P: n/a
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)+math.cos(y))()

Kay

Jun 1 '07 #14

P: n/a
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
Jun 1 '07 #15

P: n/a
Steve Howell wrote:
>
The compiler doesn't know the types up front, but if
you wanted to do this kind of optimization (and you
believed that 95% of x*x cases would benefit from it,
and you're willing to sacrifice performance for the 5%
of folks that overload multiply), then the compiler
could generate bytecode that set the stage for later
conditional caching of the first execution of x*x.
True.
You'd then need the execution of the bytecodes at
runtime (ceval.c or something called by it) to work in
such a way that they only cache the result when side
effects are not an issue. At runtime you can reliably
detect whether something is still a virgin builtin,
correct?
I've no idea, but I imagine that psyco knows whether or not it has a
proper built-in number object when it generates specialised code for
similar cases.
To my disclaimer, you would only undertake such an
optimization if multiplication were really, really
expensive (which I don't think is even true for floats
today), and even then you'd proceed cautiously.
Indeed. Some believe that for "full Python" you can only introduce
such measures at run-time, although extensive enough analysis of the
code could perhaps suggest suitable specialisations in advance, as
presumably demonstrated by Shed Skin.

Paul

Jun 1 '07 #16

P: n/a
On Fri, 01 Jun 2007 07:09:50 -0400, Steve Holden wrote:
>>>>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.

The F-bot once suggested adding a clause to the Zen of Python about
"writing two lines of code is not a sin" or "cramming two lines of code
into one is not a virtue" (my paraphrases).
Check the two alternatives:

def f(x):
y = x*x
return sin(y) + cos(y)

44 key presses, including tabs and newlines and a blank line after the
function, but excluding counting the shift key separately.

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

42 key presses.

Apart from the extremely minor issue of "namespace pollution", I think
that speaks for itself.

--
Steven.

Jun 2 '07 #17

P: n/a
....
After years of discussion, Guido has decided
to leave lambda alone for 3.0.

It will not be neither expanded, nor removed, nor renamed.
But it still will be as ugh, ugh, ugh-lee
as a mule walking backwards ..... ;-)
--
Stanley C. Kitching
Human Being
Phoenix, Arizona
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jun 2 '07 #18

P: n/a

"Cousin Stanley" <co***********@hotmail.comwrote in message
news:11**************@sp12lax.superfeed.net...
|
| ....
| After years of discussion, Guido has decided
| to leave lambda alone for 3.0.
| >
| It will not be neither expanded, nor removed, nor renamed.
|
| But it still will be as ugh, ugh, ugh-lee
| as a mule walking backwards ..... ;-)

Then pretend it was eliminated, as Guido once thought to do, and do not use
it. And look away when others do ;-)

tjr


Jun 2 '07 #19

P: n/a
>
>
Check the two alternatives:

def f(x):
y = x*x
return sin(y) + cos(y)

44 key presses, including tabs and newlines and a blank line after the
function, but excluding counting the shift key separately.

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

42 key presses.

Apart from the extremely minor issue of "namespace pollution", I think
that speaks for itself.
and now I've only 60 lines on my screen,
so what about

def f(x): y = x*x; return sin(y)+cos(y);

cheers,
Stef Mientki
Jun 2 '07 #20

P: n/a
St*******@gmail.com <St*******@gmail.comwrote:
On 3 , 22:07, "Steban...@gmail.com" <Steban...@gmail.comwrote:

angle is a ratio of two length and
dimensionless.http://en.wikipedia.org/wiki/Angle#U...easure_for_ang
les

only dimensionless values can be a argument of a sine and exponent!
Are you discordant?

if you are discordant read more :P :
sine is a dimensionless value.
if we expand sine in taylor series sin(x) = x - (x^3)/6 + (x^5)/120
etc.
you can see that sin can be dimensionless only if x is dimensionless
too.

I am a professional physicist and a know about what I talk
Lots of people are confused by the concept of "degrees" -- your Taylor
series, of course, intrinsically assumes x is "in radians" (which of
course IS how angles "truly are"). I blame the Babylonians for that
confusion just as much as for the clunky base-60 that intrudes in our
ordinary time reckoning...!
Alex
Jun 4 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.