采访:什么是Lambda表达? [英] Interview: What is Lambda expression?

查看:63
本文介绍了采访:什么是Lambda表达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近接受了两次电话采访.

I have had recently two telephone interviews.

在两次采访中,我都被问到最后一个定义Lambda表达式的问题.

In both interviews I have been asked as the last question to define a Lambda expression.

我声称Lambda表达式是一种未命名的方法,代替了代理. 但是以某种方式还不够.

I claimed that Lambda expression is an unnamed method in place of a delegate. But somehow that wasn't enough.

我很难在电话采访中准确解释这一点.

I find it very hard to explain this precisely on a telephone interview.

有人知道吗?

推荐答案

Lambda表达式是作为常量值给出的无名函数.它们可以出现在任何其他常量可能出现的任何位置,但是通常作为参数写入某些其他函数.典型的例子是,您将比较函数传递给通用的"sort"例程,而不用麻烦去定义一个完整的函数(并引起词法不连续和命名空间污染)来描述这种比较,传递一个描述比较的lambda表达式.

Lambda Expressions are nameless functions given as constant values. They can appear anywhere that any other constant may, but are typically written as a parameter to some other function. The canonical example is that you'll pass a comparison function to a generic "sort" routine, and instead of going to the trouble of defining a whole function (and incurring the lexical discontinuity and namespace pollution) to describe this comparison, you can just pass a lambda expression describing the comparison.

但是,这错过了Lambda表达式最重要的功能之一,那就是它们在外观上下文中执行.因此,他们可以使用在该上下文中定义的变量的值.这将功能指针与真正的lambda表达式区分开.在支持可变变量的语言中,适当的lambda表达式可以更改这些变量的值.

HOWEVER, this misses one of the most important features of Lambda Expressions, which is that they execute in the context of their appearance. Therefore, they can use the values of the variables that are defined in that context. This differentiates function-pointers from true lambda expressions. In languages supporting mutable variables, proper lambda expressions offer the power to change the values of those variables.

Lambda表达式(使用不同的语法)出现在所有LISP,Perl,Python以及最新版本的C ++,Objective C,C#和Java 8中,但值得注意的是,即使它有处理传递的方法,它也不会在C中出现函数(或它们的借口)作为参数.它们是具有特定语义的语法元素,这些语义对运行时的要求比C所设计的要高.

Lambda expressions appear (with different syntax) in all LISPs, Perl, Python, and sufficiently-recent versions of C++, Objective C, C# and Java 8, but notably not in C even though it has a way to deal with passing functions (or some excuse for them) around as parameters. They are a syntax element with particular semantics, and those semantics place more requirements on the runtime than C was designed to require.

这篇关于采访:什么是Lambda表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆