Now and then I use the Python lambda. Is it so formal that it is safe to say that you can do formal lambda calculus with it? I just used it but I didn't fully understand whether the python lambda and the lambda calculus like I read was done by Alonzo Church. I also used it in Javascript, I think. Isn't this more common in functional languages (e.g. Haskell, Scheme/Lisp, Clojure…) and I never saw lambda in use with Java or C(++)?
Is says on this site " a function you can pass on to another function as argument.".
But how do I get used to it enough so that I can tell where to benefit from it? If I can do it in say SQL, JPQL or GQL instead, shouldn't I prefer to do it in the query language?
Best Answer
Python
lambda
expressions are real, formal untyped λ-calculus lambda expressions.They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.
You use them wherever a
lambda
is more suitable and convenient than a full function definition. The pythondef functionname(argumentlist):
syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. Alambda
on the other hand, is an expression, so you can use alambda
to insert a callback function inline:The above example consists only of an expression. The python
map()
function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using alambda
expression to define that callable is much easier than using a function statement:For the full function syntax I need to assign a name, put the function definition on separate lines, and use the
return
statement to return the result of the expression.