Is this a good use for lambda

Discussion in 'Python' started by Charlie Taylor, Dec 17, 2004.

  1. 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?
     
    Charlie Taylor, Dec 17, 2004
    #1
    1. Advertising

  2. 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
     
    Harlin Seritt, Dec 18, 2004
    #2
    1. Advertising

  3. 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
     
    Steven Bethard, Dec 18, 2004
    #3
  4. Charlie Taylor

    Jeff Shannon Guest

    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
     
    Jeff Shannon, Dec 18, 2004
    #4
  5. 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
     
    Michael Hoffman, Dec 18, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Smith

    Finally found a use for lambda!

    Roy Smith, Sep 15, 2003, in forum: Python
    Replies:
    9
    Views:
    357
    Follower
    Sep 16, 2003
  2. Fredrik Lundh

    Re: Is this a good use for lambda

    Fredrik Lundh, Dec 19, 2004, in forum: Python
    Replies:
    24
    Views:
    639
    Terry Reedy
    Dec 23, 2004
  3. Roman Suzi
    Replies:
    13
    Views:
    620
    Bengt Richter
    Jan 7, 2005
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    194
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    258
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page