Lambda 函数
我真的很感兴趣 lambda 函数的使用方式。在像 php 这样的现代高级编程语言中使用它们有意义吗?如果是,为什么?
这真的只是一个嵌入函数中的函数(像这样)还是还有更多在他们后面?
I'm really interested how lambda functions are used. Does it make sense to use them in a modern, high-level programming language like php? If yes, why?
Is this really just a function embedded in a function (like this) or is there more behind them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
lambda 项将函数表示为值。它可以完全作为一个值传递(因为它本身就是一个值),并且可以通过给它一些参数来应用它。命名函数只是映射到 lambda 项的名称,可以是常量,也可以是变量(取决于语言细节)。
是的,它们在高级编程语言中相当普遍。例如,JavaScript 中的所有函数实际上都是存储在变量中的 lambda 项。
A lambda term represents a function as a value. It can be passed around exactly as a value (as it is one) and yet applied by giving it some arguments. Named functions are just names that map to lambda terms, either as constants or variables (depending on the language details).
And yes, they are quite prevalent in high-level programming languages. For example, all functions in JavaScript are actually lambda terms stored in variables.
lambda 函数是一个对象(或值)。它可以作为参数传递给另一个函数,也可以从一个函数返回。
它不需要名字;当您使用(例如在 Python 中)
lambda x: x**2
创建一个时,您就拥有了 lambda 函数。将其与更传统的方法进行对比:也许最重要的是,您可以在运行时构建它们。
A lambda function is an object (or value). It can be passed as an argument to another function, or returned from one.
It doesn't need a name; when you create one with (e.g. in Python)
lambda x: x**2
, you have the lambda function right there. Contrast that with the more conventional:Perhaps most importantly, you can construct them during runtime.
虽然 PHP 支持匿名函数,但它们实际上并不是闭包——所以你会错过最有趣的部分。
在 JavaScript(以及支持“lambdas”正确方式的语言)中,您可以从函数返回一个函数,并且即使在外部函数完成执行之后,返回的函数也能够访问封闭的上下文。例子:
While PHP supports anonymous functions, they are not actually closures -- so you miss the most interesting part.
In JavaScript (and languages that support "lambdas" The Right Way) you can return a function from a function, and the returned one would be able to access the enclosing context even after the outer function has finished execution. Example:
关于 lambda 函数有两个重要的事情(至少是该术语的常用方式):
它们可以传递。函数可以将 lambda 函数作为参数或返回 lambda 函数。
它们可以访问定义它们的范围。因此,例如,lambda 函数可以访问封闭函数的参数。
作为说明其用途的基本示例,我将用 Ruby 编写一个
filter
函数:我们的
filter
不知道我们将使用什么标准进行过滤。该逻辑全部包含在我们传入的块(Ruby 版本的 lambda 函数)中。但请注意,即使 lambda 函数实际上是在filter
内部调用的,它仍然知道该位置的阈值它是在哪里定义的。There are two important things about lambda functions (at least the way the term is commonly used):
They can be passed around. A function can take a lambda function as an argument or return a lambda function.
They have access to the scope where they're defined. So, for example, the lambda function can access the enclosing function's arguments.
For a basic example of how this is useful, I'll write a
filter
function in Ruby:Our
filter
doesn't know what criteria we're going to use for filtering. That logic is all contained in the block (Ruby's version of a lambda function) that we pass in. But notice that even though the lambda function is actually called insidefilter
, it still knows the threshold from the place where it was defined.它基本上是语法糖化,你用 10 行委托编写的内容,以及你拥有的内容,都可以在一行 lambda 表达式中完成。对我来说,要考虑的主要事情是,当与多个开发人员在同一项目上合作时,您必须确保其可读性仍然清晰。
您可以内联附加一个事件
Button1_Click += (sender, eventArgs) =>; { //myCode };
但是您将无法重用该事件,并且如果其中有大量代码,则无助于保持代码透明。
您还可以创建一个 lambda 来从 List<> 检索数据,但是当有很多参数时,这可能会变得相当不清楚,而一个好的 LINQ 查询可以更清晰。
这主要是个人选择,但我必须说我已经使用过它很多次了:)
希望这能有所帮助,
山姆
It's basically syntactic sugaring, what you would write in 10 lines with delegates and what have you, can be done in a one-line lambda expression. The main thing to take into account for me, is when working with multiple developers on the same project, that you have to make sure its readability is still clear.
You could attach an event inline
Button1_Click += (sender, eventArgs) => { //myCode };
But you won't be able to reuse the event, and if there's a lot of code in there, it won't help in keeping your code transparant.
You could also create a lambda for retrieving data from a List<>, but that can become quite unclear when there's a lot of parameters, where a nice LINQ Query can be a lot more clear.
It's mostly a personal choice but I must say I have used it quite a few times :)
Hope this helps a little,
Sam