lambda

Morbid Curiosity morbid at doesnotlikespam.inet.net.nz
Wed Mar 29 12:19:04 CEST 2000


On 28 Mar 2000 22:37:53 -0800, Falknor <falknor at worldspy.net> wrote:
>Hmm, what exactly is the use of lambda?  It just isn't clickin for
>some reason....  Are they just syntactical candy (such as the ternary
>operator ?: in C++ is syntactical candy..) or what. *ponders*  Anyone
>who could explain it, and gimme a little example would have my thanx. :)

Lambda functions come from a functional programming paradigm, but I've
found a couple of situations where they're really handy in a general
imperative program, too. Have a look at this code snippet:

-----
>>> def rng(a,b):
...	return map( (lambda(x):x*0.1), range(a/0.1,b/0.1))
...
>>> range(-1,1)
[-1, 0]
>>> rng(-1,1)
[-1.0, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1,
0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

-----

Using the lambda() and map() functions, we can get fractional ranges
for use in thing like for() statements. You can do it by writing a
div_by_ten(x) function, but that does tend to clutter the name-space a
bit.

Look useful, yet?

Morbid




More information about the Python-list mailing list