匿名函数的用处

发布于 2024-07-26 11:40:55 字数 38 浏览 6 评论 0原文

为什么不只写匿名函数内容而不是写匿名函数AND匿名函数内容呢?

Why not write the anonymous function content only instead of the anonymous function AND the anonymous function content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

暗地喜欢 2024-08-02 11:40:58

它就在那里,因此您不必创建本质上无用的函数包装器。

It is there so that you don't have to create a function wrapper that is essentially useless.

一张白纸 2024-08-02 11:40:58

它们的存在是因为很多时候您希望使用表达式语法来获得方法语义。

var l = new List<int>() { 1, 2, 3, 4, 5 };
var a = l.Where( i => i > 3 );  // a = 4, 5

我不想编写一个新的方法谓词来检查整数是否大于三。 我想要一个只能放置表达式的方法的含义。 匿名函数让我两者兼得。 哇! :)

They exist because there are lots of times you want to have method semantics with expression syntax.

var l = new List<int>() { 1, 2, 3, 4, 5 };
var a = l.Where( i => i > 3 );  // a = 4, 5

I didn't want to bother writing a new method predicate that checked if an integer was greater than three. I want the meaning of a method where I can only put an expression. The anonymous function lets me have both. Woot! :)

电影里的梦 2024-08-02 11:40:58

如果您问“为什么使用 lambda 表达式?”,那么有几个答案。 其一,作为简写形式的 lambda 表达式通常比完整的匿名函数(即使用 delegate() 术语)或完整的函数和委托的更冗长的替代方案更容易阅读和理解。 Lambda 表达式也非常适合执行简短、简单、“富有表现力”的计算(即对一组输入进行简单的数学计算),这在软件编程中非常常见。

Lambda 表达式是一个强大的工具,可以帮助提高代码的表达性、简单性和可读性。

If you are asking "Why use a lambda expression?", then there are a few answers. For one, lambda expressions, being shorthand, are usually easier to read and understand than the much more verbose alternatives of full anonymous functions (i.e. with the delegate() term), or a full blown function and delegate. Lambda expressions are also ideal for performing short, simple, "expressive" computations (i.e. simple mathematics on a set of input), which is quite frequent in software programming.

Lambda expressions are a powerful tool that can help improve the expressiveness, simplicity, and readability of your code.

a√萤火虫的光℡ 2024-08-02 11:40:57

最大的用途可能是一次性函数,这些函数不会在其他任何地方使用,或者需要根据需要动态创建。 这样,他们就不需要“污染”命名空间。

例如,在 python 中:

x_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_values = map(lambda x: x**x + 2x + 3, x_values)

通过使用 lambda,我不需要在模块命名空间中仅针对一个多项式 (x^2 + 2x + 3) 创建函数。

它们在很多领域都可以派上用场,特别是在函数式编程中(例如查找闭包或柯里化)。

Probably the biggest use is for one-off functions that won't be used anywhere else or need to be created on demand, dynamically. That way, they don't need to "pollute" the namespace.

For example, in python:

x_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_values = map(lambda x: x**x + 2x + 3, x_values)

By using the lambda, I don't need to create a function in the module namespace just for one polynomial (x^2 + 2x + 3).

There are many areas where they come in handy, especially in functional programming (lookup closures or currying, for example).

夜声 2024-08-02 11:40:57

我不知道这是否适用,因为它是特定于 Javascript 的,而且我没有看到 Javascript 标签,但是......一次创建+调用匿名函数并不罕见。 这样做通常是为了保护全球范围免受可变污染。

var x = 1;
(function(){        
    var x = 2;
})();
x == 1;

I don't know if this even applies, since it is specific to Javascript and I don't see a Javascript tag, but....it's not uncommon to create+call an anonymous function at once. It's often done in the sake of preserving the global scope from variable pollution.

var x = 1;
(function(){        
    var x = 2;
})();
x == 1;
吃→可爱长大的 2024-08-02 11:40:56

因为您不会编写匿名函数然后立即调用它。 它通常被传递给其他一些用它来做事的函数(例如使用匿名函数将一个数组映射到另一个数组)。

Because you don't write an anonymous function then immediately call it. It's usually passed to some other function that does things with it (e.g. uses the anonymous function to map an array to another array).

温折酒 2024-08-02 11:40:56

它们通常用于回调。 你不能只写内容,因为你的代码不会直接执行它。

They're commonly used for callbacks. You can't just write the content, because your code doesn't execute it directly.

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