匿名函数的用处
为什么不只写匿名函数内容而不是写匿名函数AND匿名函数内容呢?
Why not write the anonymous function content only instead of the anonymous function AND the anonymous function content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它就在那里,因此您不必创建本质上无用的函数包装器。
It is there so that you don't have to create a function wrapper that is essentially useless.
它们的存在是因为很多时候您希望使用表达式语法来获得方法语义。
我不想编写一个新的方法谓词来检查整数是否大于三。 我想要一个只能放置表达式的方法的含义。 匿名函数让我两者兼得。 哇! :)
They exist because there are lots of times you want to have method semantics with expression syntax.
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! :)
如果您问“为什么使用 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.
最大的用途可能是一次性函数,这些函数不会在其他任何地方使用,或者需要根据需要动态创建。 这样,他们就不需要“污染”命名空间。
例如,在 python 中:
通过使用 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:
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).
我不知道这是否适用,因为它是特定于 Javascript 的,而且我没有看到 Javascript 标签,但是......一次创建+调用匿名函数并不罕见。 这样做通常是为了保护全球范围免受可变污染。
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.
因为您不会编写匿名函数然后立即调用它。 它通常被传递给其他一些用它来做事的函数(例如使用匿名函数将一个数组映射到另一个数组)。
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).
它们通常用于回调。 你不能只写内容,因为你的代码不会直接执行它。
They're commonly used for callbacks. You can't just write the content, because your code doesn't execute it directly.