我真的不获得 lambda表达式。自从ALGOL的日子开始,我们才开始听到他们的观点,直到最近才发现Python和Ruby变得非常受欢迎。现在C#有 => 语法,我的世界(.NET)中的人越来越多地在谈论lamdba表达式。
I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more.
我已经阅读了关于lambda演算的维基百科文章,但我并不是一个数学家。从实际的角度来看,我真的不了解它。什么时候我会使用lambda表达式?为什么?我如何知道这是我应该在做什么?
I've read the Wikipedia article on the lambda calculus, but I'm not really a math guy. I don't really understand it from a practical perspective. When would I use lambda expressions? Why? How would I know that it's what I should be doing?
你能否以前后格式显示如何解决lambda表达式问题的示例?任何命令式语言都很好,但C#对我来说最容易理解。
Can you show examples of how you would solve problems with lambda expressions, in a before-and-after format? Any imperative language is fine, but C# would be easiest for me to understand.
推荐答案基本上,就C#而言,lambda表达式是创建代理(或表达式树,但现在让我们放在一边的)的简单方法。
Basically as far as C# is concerned, lambda expressions are an easy way to create a delegate (or an expression tree, but let's leave those aside for now).
在C#1中,我们只能从正常创建委托实例方法。 在C#2中,我们获得了匿名方法。 在C#3中,我们获得了lambda表达式,这就像更简洁的匿名方法。
In C# 1 we could only create delegate instances from normal methods. In C# 2 we gained anonymous methods. In C# 3 we gained lambda expressions, which are like more concise anonymous methods.
当你想表达一些需要一个逻辑的逻辑时,它们特别简洁值并返回一个值。例如,在LINQ的上下文中:
They're particularly concise when you want to express some logic which takes one value and returns a value. For instance, in the context of LINQ:
// Only include children - a predicate var query = dataSource.Where(person => person.Age < 18) // Transform to sequence of names - a projection .Select(person => person.Name);有一个更全面的讨论与其他方面 - 关于关闭的文章。
There's a fuller discussion of this - along with other aspects - in my article on closures.
更多推荐
你可以解释lambda表达式吗?
发布评论