473,473 Members | 4,204 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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?
Jun 1 '07 #1
20 2144
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

"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
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

"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
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

"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
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
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
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
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
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

"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
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
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
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
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
....
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

"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
>
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Sakkis | last post by:
The S-expression parser below works, but I wonder if it can be simplified; it's not as short and straightforward as I would expect given the simplicity of S-expressions. Can you see a simpler...
3
by: Jacob Weber | last post by:
If I do a search with a regular expression that uses parenthesized subexpressions, is there a way to find out where it found the subexpressions? For example, say I do: "bb".search(/b(.)/g); ...
14
by: Clay_Culver | last post by:
My professor claims that this code: int f() { int x = 5; int &y = x; x += ++y; return x; }
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
0
by: sterling | last post by:
Here's the deal, the following expression only displays a range. Sometimes the text I'm searching has more that one returned Grouping I need to return only one group by adding to the expression...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
111
by: Jordan Abel | last post by:
Is this defined or not? Some people in ##c are saying that it has to result in x being set to 11, i'm saying it's undefined. Who's right?
0
by: tonychestnut | last post by:
Hi, I'm trying to write an interesting bit of code that treats a Boost bind expression as a subexpression in a TR1 bind expression. So, I was trying to do so by specializing is_bind_expression....
45
by: azrael | last post by:
Hy guys, A friend of mine i a proud PERL developer which always keeps making jokes on python's cost. Please give me any arguments to cut him down about his commnets like :"keep programing i...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.