Re: [web2py] Re: When to use "lambda"?

2010-12-21 Thread Branko Vukelić
On Wed, Dec 22, 2010 at 12:13 AM, Stefaan Himpe wrote: >> myfunction(lambda x: print 'Eat %s!' % x) >> >> and that would print 'Eat this!'. > > Ahm... :) > You can't use print in lambda as it is a statement, not an expression (in > python 2.x at least) Yah, I know. I've corrected myself. But I gu

[web2py] Re: When to use "lambda"?

2010-12-21 Thread Stefaan Himpe
myfunction(lambda x: print 'Eat %s!' % x) and that would print 'Eat this!'. Ahm... :) You can't use print in lambda as it is a statement, not an expression (in python 2.x at least)

[web2py] Re: When to use "lambda"?

2010-12-21 Thread cjrh
On Dec 21, 11:53 pm, cjrh wrote: > Because Python's scoping rules are strict and clean, we can even do > the following: > > my_list_of_functions = [lambda x, i=i : x + j for i in range(10)] To my shame, this should have been: my_list_of_functions = [lambda x, i=i : x + i for i in range(10)] Apo

Re: [web2py] Re: When to use "lambda"?

2010-12-21 Thread Jonathan Lundell
On Dec 21, 2010, at 1:37 PM, cjrh wrote: > > On Dec 21, 10:36 pm, pbreit wrote: >> I see lambda used quite a bit and don't totally understand the concept. Is >> there a simple rule to follow to know when it is necessary to use? > Another answer to the original question is that there are places

[web2py] Re: When to use "lambda"?

2010-12-21 Thread cjrh
On Dec 21, 11:37 pm, cjrh wrote: > def g(i): >     def f(x): >         return x + i >     return f > my_list_of_functions = [g(i) for i in range(10)] I suspect the following won't be of immediate use to you, but it is worth hearing about right now, even if it'll only make sense sometime later in

[web2py] Re: When to use "lambda"?

2010-12-21 Thread cjrh
On Dec 21, 10:36 pm, pbreit wrote: > I see lambda used quite a bit and don't totally understand the concept. Is > there a simple rule to follow to know when it is necessary to use? Code A: f = lambda x,y: x + y Code B: def f(x,y): return x + y If you can remember that Code A is *exactly*

[web2py] Re: When to use "lambda"?

2010-12-21 Thread Anthony
And if you're interested, the name "lambda" itself comes from lambda calculus: http://en.wikipedia.org/wiki/Lambda_calculus http://en.wikipedia.org/wiki/Lambda_calculus#First-class_functions http://en.wikipedia.org/wiki/Anonymous_functions On Tuesday, December 21, 2010 4:02:36 PM UTC-5, stefa

[web2py] Re: When to use "lambda"?

2010-12-21 Thread Stefaan Himpe
pbreit wrote: I see lambda used quite a bit and don't totally understand the concept. Is there a simple rule to follow to know when it is necessary to use? Sometimes you need to pass a function as an argument to another function. In that case you have a choice to 1. define a separate functio

[web2py] Re: When to use "lambda"?

2010-12-21 Thread Arun K.Rajeevan
lambda is functional programming bit. :-) Well, you can use lambda to 1) define anonymous functions 2) one liner functions 3) to modify a function to behave the way you want (instead of defining another function that just call this function with desired params) etc, to name just few, that came i