473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is this a good use for lambda


I find that I use lambda functions mainly for callbacks to things like
integration or root finding routines as follows.

flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd)

root = findRoot(xBeg, xEnd,
lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)

I have tried using named functions instead of using lambda functions,
however, I always end up with a convoluted, hard to follow mess.

Is there a better solution than a lambda in the above situations?

Jul 18 '05 #1
4 1496
Charlie Taylor wrote:

I find that I use lambda functions mainly for callbacks to things like
integration or root finding routines as follows.

flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd)

root = findRoot(xBeg, xEnd,
lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)

I have tried using named functions instead of using lambda functions,
however, I always end up with a convoluted, hard to follow mess.

Is there a better solution than a lambda in the above situations?


Yes. Write a separate function for it. It may actually take less time and be
a good deal more readable. Also, you'll be able to call this mess again if
you need to. :-)

--
Harlin Seritt
Jul 18 '05 #2
Harlin Seritt wrote:
Charlie Taylor wrote:

I find that I use lambda functions mainly for callbacks to things like
integration or root finding routines as follows.

flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd)

root = findRoot(xBeg, xEnd,
lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)

I have tried using named functions instead of using lambda functions,
however, I always end up with a convoluted, hard to follow mess.

Is there a better solution than a lambda in the above situations?

Yes. Write a separate function for it. It may actually take less time and be
a good deal more readable. Also, you'll be able to call this mess again if
you need to. :-)


Well, I think the jury could still be out on which version is more
readable, but I don't understand the comment "I have tried using named
functions instead of using lambda functions, however, I always end up
with a convoluted, hard to follow mess." If you know that:

<name> = lambda *args, **kwds: <expr>

is eqivalent to:

def <name>(*args, **kwds):
return <expr>

then it's quite straightforward to translate from one to the other. As
an illustration, here are your functions translated from lambdas to defs:

def flowfunc(x):
return 2.0*pi * d(x)* v(x) * sin(a(x))
flow = integrate(flowfunc, xBeg, xEnd)

def rootfunc(x):
return y2 + lp*(x - x2) - wallFunc(x)[0]
root = findRoot(xBeg, xEnd, rootfunc, tolerance=1.0E-15)

I'm not necessarily suggesting that this is the right answer for your
situation, just that the translation from lambda to def should be
relatively simple if you decide that that's what you want to do.

Steve
Jul 18 '05 #3
Charlie Taylor wrote:
root = findRoot(xBeg, xEnd,
lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)


Um, so which parts of this are the actual lambda?? Just from reading
that, it's hard to be sure. My mind keeps wanting to break at 'lambda
x: y2 + lp*(x-x2)', but when I stop to think about it, I know that it
must be the entire segment between commas ('lambda x: y2 + lp*(x-x2)
-wallFunc( x )[0]').

This is exactly why I don't like using lambdas. Very easy to get
confused by the syntax, and (IMO) not much benefit.
I have tried using named functions instead of using lambda functions,
however, I always end up with a convoluted, hard to follow mess.


See, to my mind, the above is a bit convoluted and hard to follow. I'd
prefer to see something like:

def func(x):
answer = y2 + (lp * (x-x2)) - wallFunc(x)[0]
return answer

root = findRoot(xBeg, xEnd, func, tolerance=1.0E-15)

(I'm hoping, of course, that y2, x2, and lp are local variables, rather
than global variables...)

I find this named function to be much more clear in regards to what's
part of the lambda and what's actually a parameter to findRoot(). I
suppose that opinions may vary, however.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #4
Charlie Taylor wrote:
flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd)
def _flow_func(x):
return 2.0 * pi * d(x) * v(x) * sin(a(x))
flow = integrate(_flow_func, xBeg, xEnd)
root = findRoot(xBeg, xEnd,
lambda x: y2+ lp*(x-x2) -wallFunc( x )[0], tolerance=1.0E-15)


def _root_func(x):
return y2 + lp*(x - x2) - wallFunc(x)[0]
root = findRoot(xBeg, xEnd, _root_func, tolerance=1.0e-15)

I think those are much easier to follow. I find consistent punctuation
spacing helps readability too...
--
Michael Hoffman
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.