Lambda 函数

发布于 2024-09-04 03:09:12 字数 197 浏览 3 评论 0原文

我真的很感兴趣 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝礼 2024-09-11 03:09:12

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.

宣告ˉ结束 2024-09-11 03:09:12

lambda 函数是一个对象(或值)。它可以作为参数传递给另一个函数,也可以从一个函数返回。

map(lambda x: x**2, [1,2,3,4])

它不需要名字;当您使用(例如在 Python 中)lambda x: x**2 创建一个时,您就拥有了 lambda 函数。将其与更传统的方法进行对比:

def square(x):
    return x**2

也许最重要的是,您可以在运行时构建它们。

def make_exp_function(n):
    return lambda x: x**n

square = make_exp_function(2)
cube = make_exp_function(3)
for i in range(100):
    f = make_exp_function(i)
    print f(2)

A lambda function is an object (or value). It can be passed as an argument to another function, or returned from one.

map(lambda x: x**2, [1,2,3,4])

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:

def square(x):
    return x**2

Perhaps most importantly, you can construct them during runtime.

def make_exp_function(n):
    return lambda x: x**n

square = make_exp_function(2)
cube = make_exp_function(3)
for i in range(100):
    f = make_exp_function(i)
    print f(2)
满栀 2024-09-11 03:09:12

虽然 PHP 支持匿名函数,但它们实际上并不是闭包——所以你会错过最有趣的部分。

在 JavaScript(以及支持“lambdas”正确方式的语言)中,您可以从函数返回一个函数,并且即使在外部函数完成执行之后,返回的函数也能够访问封闭的上下文。例子:

function counter(start){
    return function() {
        return start++;
    };
};

var f = counter(1);
alert( f() ); // now it's 1
alert( f() ); // now it's 2
alert( f() ); // now it's 3
var g = counter(10);
alert( g() ); // now it's 10
alert( g() ); // now it's 11
alert( f() ); // now it's 4!

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:

function counter(start){
    return function() {
        return start++;
    };
};

var f = counter(1);
alert( f() ); // now it's 1
alert( f() ); // now it's 2
alert( f() ); // now it's 3
var g = counter(10);
alert( g() ); // now it's 10
alert( g() ); // now it's 11
alert( f() ); // now it's 4!
鯉魚旗 2024-09-11 03:09:12

关于 lambda 函数有两个重要的事情(至少是该术语的常用方式):

  1. 它们可以传递。函数可以将 lambda 函数作为参数或返回 lambda 函数。

  2. 它们可以访问定义它们的范围。因此,例如,lambda 函数可以访问封闭函数的参数。

作为说明其用途的基本示例,我将用 Ruby 编写一个 filter 函数:

def filter(array, &lambda_function)
  new_array = []
  for item in array
    new_array.push(item) if lambda_function[item]
  end
  return new_array
end

$people_array = #imagine I have an array of Person objects here

def find_people_richer_than(threshold)
  return filter($people_array) {|person| person.wealth >= threshold}
end

我们的 filter 不知道我们将使用什么标准进行过滤。该逻辑全部包含在我们传入的块(Ruby 版本的 lambda 函数)中。但请注意,即使 lambda 函数实际上是在 filter 内部调用的,它仍然知道该位置的阈值它是在哪里定义的。

There are two important things about lambda functions (at least the way the term is commonly used):

  1. They can be passed around. A function can take a lambda function as an argument or return a lambda function.

  2. 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:

def filter(array, &lambda_function)
  new_array = []
  for item in array
    new_array.push(item) if lambda_function[item]
  end
  return new_array
end

$people_array = #imagine I have an array of Person objects here

def find_people_richer_than(threshold)
  return filter($people_array) {|person| person.wealth >= threshold}
end

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 inside filter, it still knows the threshold from the place where it was defined.

む无字情书 2024-09-11 03:09:12

它基本上是语法糖化,你用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文