LAMBDA IS IT USELESS?
Suchandra Thapa
ssthapa at classes.cs.uchicago.edu
Fri Jul 13 09:28:00 EDT 2001
EricIDLE <grayson-wilson at home.com> wrote:
>Ok well I dont quite grasp the Lambda concept heres an example.
[...]
>
>Isnt that easier??
>
>Or do I not get lambda?
>
>*NOTE ... I think its just a way to complicate things OR just a way to avoid
>typing the function name beacuse.... I dont know they are scared of the
>names they give them.
The lambda can be a way to avoid the need to define and name trivial
functions that get used in only one place. However, the lambda function
is a also a way to dynamically create functions as well as a way of
composing two functions together. For example, suppose you need to
apply a scaling factor to a list but the scaling factor depends on
user input. In this case, with lambda you can do a with
lambda x: x * s where the value of s depends on the user input. Also,
suppose you have two image filters and you want to create a new filter
that applies the first filter then the second one. Then using lambda,
you can just use lambda x: filter2(filter1(x)).
Incidentally, you can use lambda to do a bunch of different things including
reducing the number of parameters a function takes (currying). E.g.
suppose foo takes x, y, and z then lambda x, y:foo(x, y, 4) returns a function
that takes only two paramters.
Basically, think of lambda as a way of applying mathematical operations
like +, /, * as well as a few others to functions.
--
----------------------------------------------------------------------------
|
Suchandra Thapa | "There are only two kinds of math .
s-thapa-11 at NOSPAMalumni.uchicago.edu | books. Those you cannot read beyond
| the first sentence, and those you
| can not read beyond the first page."
| -C.N. Yang
----------------------------------------------------------------------------
More information about the Python-list
mailing list