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? 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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. :-)
|
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...
|
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
|
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...
| |
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
|
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...
|
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
|
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>
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |