问题:
What do lambda expressions do?
Can we write all functions as lambda expressions?
In what cases are lambda expressions useful?
个人答案:
lambda是一个具名函数的匿名化,具名函数的意义是表达某一个数据转换,函数有了名字就可以被外部程序调用,而lamda的位置就是一个函数的参数位置,当函数可以做为值传递时,应该使用lambda,lamda的一个作用就是捕获局部变量,在运行时可以拿到捕获变量的值,实现一些魔法操作
具名函数是可以别人的使用者调用,所以lamba不能代替具名函数,但是某一个模块功能在理论是可以全部使用lamba实现的(通过函数柯里化与高阶函数)。
当函数作为值传递时,应该使用lambda
标准答案(in python?):
Lambda expressions create functions. When a lambda expression is evaluated, it produces a function. We often use lambdas to create short anonymous functionsthat we won't need for too long.
We can't write all functions as lambda expressions because lambda functions all have to have return statements and they can't contain very complex multi-lineexpressions.