lambda 表达式有什么意义? [英] What's the point of a lambda expression?
问题描述
阅读这篇文章后,我不明白为什么曾经使用过 lambda 表达式.公平地说,我不认为我对什么是委托和表达式树类型有正确的理解,但我不明白为什么有人会使用 lambda 表达式而不是声明的函数.有人能指教我吗?
After reading this article, I can't figure out why lambda expressions are ever used. To be fair, I don't think I have a proper understanding of what delegates and expression tree types are, but I don't understand why anyone would use a lambda expression instead of a declared function. Can someone enlighten me?
推荐答案
第一:简洁性和局部性:
您更愿意编写、阅读和维护哪个?这:
Which would you rather write, read and maintain? This:
var addresses = customers.Select(customer=>customer.Address);
或:
static private Address GetAddress(Customer customer)
{
return customer.Address;
}
... a thousand lines later ...
var addresses = customers.Select(GetAddress);
当您可以将需要的代码放在需要的地方作为简短的表达式时,用成百上千的四行函数把程序弄得乱七八糟有什么意义?
What's the point of cluttering up your program with hundreds or thousands of four-line functions when you could just put the code you need where you need it as a short expression?
第二:lambda 关闭局部作用域
你更愿意读、写和维护哪个,这个:
Which would you rather read, write and maintain, this:
var currentCity = GetCurrentCity();
var addresses = customers.Where(c=>c.City == currentCity).Select(c=>c.Address);
或:
static private Address GetAddress(Customer customer)
{
return customer.Address;
}
private class CityGetter
{
public string currentCity;
public bool DoesCityMatch(Customer customer)
{
return customer.City == this.currentCity;
}
}
....
var currentCityGetter = new CityGetter();
currentCityGetter.currentCity = GetCurrentCity();
var addresses = customers.Where(currentCityGetter.DoesCityMatch).Select(GetAddress);
当您使用 lambda 时,所有烦人的代码都是为您编写的.
All that vexing code is written for you when you use a lambda.
第三:查询推导式为您重写为 lambdas
当你写作时:
var addresses = from customer in customers
where customer.City == currentCity
select customer.Address;
它已为您转换为 lambda 语法.很多人觉得这种语法读起来很愉快,但我们需要 lambda 语法才能让它真正起作用.
it is transformed into the lambda syntax for you. Many people find this syntax pleasant to read, but we need the lambda syntax in order to actually make it work.
第四:lambdas 是可选的类型推断
请注意,在上面的查询推导式或 lambda 版本中,我们不必提供客户"的类型,但在将其声明为静态方法时,我们必须提供形参的类型.编译器很聪明地从上下文推断 lambda 参数的类型.这使您的代码不那么冗余且更加清晰.
Notice that we don't have to give the type of "customer" in the query comprehension above, or in the lambda versions, but we do have to give the type of the formal parameter when declaring it as a static method. The compiler is smart about inferring the type of a lambda parameter from context. This makes your code less redundant and more clear.
第五:Lambda 可以成为表达式树
假设您想询问 Web 服务器将居住在当前城市的客户的地址发送给我".您想 (1) 从网站上拉下一百万客户并在您的客户端机器上进行过滤,还是 (2) 向网站发送一个对象,告诉它查询包含当前城市的过滤器,然后选择地址"?让服务器完成工作并只向您发送匹配的结果.
Suppose you want to ask a web server "send me the addresses of the customers that live in the current city." Do you want to (1) pull down a million customers from the web site and do the filtering on your client machine, or (2) send the web site an object that tells it "the query contains a filter on the current city and then a selection of the address"? Let the server do the work and send you only the result that match.
表达式树允许编译器将 lambda 转换为可以在运行时转换为另一种查询格式并发送到服务器进行处理的代码.在客户端上运行的小助手方法没有.
Expression trees allow the compiler to turn the lambda into code that can be transformed into another query format at runtime and sent to a server for processing. Little helper methods that run on the client do not.
这篇关于lambda 表达式有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!