473,811 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(lambd a 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 1516
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(lambd a 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(lambd a 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(flowf unc, 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(lambd a 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
3312
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 a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
53
3709
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 are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
26
3510
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 of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr> usage) These are lambdas used when a lambda wasn't needed -- an anonymous function was created with...
4
2636
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 other mathematical symbols. In my mind, i imagine that those obscure math
23
5340
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 I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
5
2166
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 bit complicated so any explanation would be appreciated. Thanks
35
1936
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 template programming. I just finished reading the first 12 chapters of _C++ Templates: The Complete Guide_. One thing that struck me as I read these chapters is how much I didn't know about C++. I _know_ the core language specification is...
21
1860
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 following function? def addn(n): return lambda x,inc=n: x+inc if i do addn(5) it returns
1
2633
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 boost::lambda::lambda_ functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10124
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.