简体   繁体   English

有什么理由让C#或Java有lambda?

[英]What reason is there for C# or Java having lambdas?

What reason is there for C# or java having lambdas? 有什么理由让C#或java有lambda? Neither language is based around them, it appears to be another coding method to do the same thing that C# already did. 这两种语言都不是基于它们,它似乎是另一种编码方法来做同样的事情,而C#已经做了。
I'm not being confrontational, if there is a reason I would like to know the reason why. 我不是对抗性的,如果有理由我想知道原因。 For the purpose of full disclosure I am a Java programmer with a C++ background with no lisp experience, I may just be missing the point. 出于完全公开的目的,我是一名具有C ++背景且没有lisp经验的Java程序员,我可能只是忽略了这一点。

There are common use-cases which require passing (or storing) a block of code to be executed later. 有一些常见的用例需要传递(或存储)稍后要执行的代码块。 The most common would be event listeners. 最常见的是事件监听器。 Believe it or not, the following bit of code uses a lambda-ish construct in Java: 信不信由你,下面的代码使用Java中的lambda-ish结构:

JButton button = new JButton("Push me!");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Pressed!");
    }
});

The anonymous inner-class is acting as a lambda, albeit an extremely verbose one. 匿名内部类充当lambda,虽然是一个非常冗长的。 With a little bit of implicit conversion magic, we can write the following equivalent in Scala: 通过一些隐式转换魔术,我们可以在Scala中编写以下等效内容:

val button = new JButton("Push me!")
button.addActionListener { e =>
  println("Pressed!")
}

C# makes this sort of thing fairly easy with delegates and (even better) lambdas. 对于代理人和(甚至更好的)lambda,C#使这种事情变得相当容易。

I see lambdas in C# as a very convenient short-cut for doing delegates. 我认为C#中的lambdas是一个非常方便的快捷方式。 Much more readable to have the code right there where it is being used rather than having to search elsewhere for the delegate definition. 将代码放在正在使用的位置,而不是在别处搜索委托定义,更具可读性。

Syntactic Sugar. 句法糖。

It provides a convenient and more-readable way to represent an idea, in this case a tiny throw-away method. 它提供了一种方便且更易读的方式来表示一个想法,在这种情况下是一个小小的丢弃方法。 Under the hood, the compiler expands that out to a delegate and method call, but it's the thing doing the work, not you. 在引擎盖下,编译器将其扩展为委托和方法调用,但这是完成工作的事情,而不是你。

Lambdas allow you write less verbose, more expressive code. Lambdas允许您编写更简洁,更具表现力的代码。 For example, list comprehensions ... 例如, 列表推导 ......

BTW, work is under way to explore the possibility of adding closures to Java - in the meantime it is necessary to use anonymous classes instead (ugly). 顺便说一下,正在研究为Java添加闭包的可能性 - 与此同时,有必要使用匿名类(丑陋)。

C# isn't going for purity to a particular school of language design (unlike Java, which was designed by the Smalltalkers as to be something of a pure OO language). 对于特定的语言设计学校而言,C#并不是纯粹的(与Java不同,Java由Smalltalkers设计为纯粹的OO语言)。 C# is going for all-things-to-all-people, and it's pretty good at it. C#适用于所有人,而且它非常擅长。 C# is based around gathering the best of the various styles of programming into one high-quality, well-supported language. C#的基础是将各种风格的编程融入一种高质量,支持良好的语言。 That includes procedural, object-oriented, functional, dynamic, logic, etc. styles of programming. 这包括程序,面向对象,功能,动态,逻辑等编程风格。 Obviously, so far it doesn't have much in the way of dynamic or logic styles of programming, but that is soon to come (dynamic programming coming with C# 4.0). 显然,到目前为止它没有太多的动态或逻辑编程风格,但很快就会出现(动态编程随C#4.0而来)。

In case of C#, lambdas are used internally to implement LINQ. 在C#的情况下,lambdas在内部用于实现LINQ。 See the article The Evolution Of LINQ And Its Impact On The Design Of C# 参见文章LINQ的演变及其对C#设计的影响

Lambda's allow for more readable code in that they allow operations to be defined closer to the point of use rather than like the current C++ method of using function objects whose definition is sometimes far from the point of use. Lambda允许更可读的代码,因为它们允许将操作定义为更接近使用点,而不是像当前使用函数对象的C ++方法那样,其定义有时远离使用点。 (This is not including some of the boost libraries). (这不包括一些增强库)。 I think the key point from lambdas is that they allow more concise and easy to understand code. 我认为lambdas的关键点在于它们允许更简洁易懂的代码。

They offer better security using the multi threading in Java by implying in many cases the "final" option. 它们使用Java中的多线程提供更好的安全性,在许多情况下暗示“最终”选项。 So you are not error prone for multitasking. 因此,您不会出于多任务处理的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM