什么是全动力闭合装置?

发布于 2024-09-09 21:01:44 字数 229 浏览 3 评论 0原文

前几天我参加了 Scala 的 Java 会议,演讲者提到了“全功能闭包”。我很难确定一个对我来说有意义的定义。我已阅读关于 closures 的维基页面,但它确实没有回答我。有人可以帮我给出一个明确的定义吗?甚至可能包括一个简单的例子。

谢谢!

I was at a Java conference on Scala the other day and the speaker referred to 'full powered closures'. I am having a hard time nailing down a definition that makes sense to me. I have read the wiki page on closures but it really didn't answer it for me. Can someone help me with a clear cut definition? Maybe even include a simple example.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终遇你 2024-09-16 21:01:45

在计算机科学中,闭包是具有自由变量的一流函数,
被绑定在词法环境中。

除了维基百科文章中的这句话之外,您不需要更多内容。 Java 缺少的(除了丑陋的语法之外)是从闭包外部绑定自由变量的功能。一个例子:

int x = 0;
new Runnable() {
    public void run() {
        x = 1;
    }
}.run();

这里Java编译器会抱怨变量x。

同样的事情在 scala 中工作得很好,而且更简洁:

var x = 0
val fun = ()=>{x=1}
fun

In computer science, a closure is a first-class function with free variables that
are bound in the lexical environment.

You don't need more than this sentence from the wikipedia article. What Java lacks (in addition to the ugly syntax) is the feature of binding free variables from outside the closure. An example:

int x = 0;
new Runnable() {
    public void run() {
        x = 1;
    }
}.run();

Here the Java compiler will complain about the variable x.

The same thing in scala works just fine and is a lot less verbose:

var x = 0
val fun = ()=>{x=1}
fun
梦晓ヶ微光ヅ倾城 2024-09-16 21:01:45

因为我感觉自己像个初学者(与 DPP 和 Amber 相比),所以我可能会用初学者语言向初学者解释它:

首先,匿名函数(或代码块/lambda 表达式)只是一个没有名称的函数。它可以与这样的变量绑定。

scala> val foo = (x: Int) => 2*x 
foo: (Int) => Int = <function1>

scala> val bar = foo
bar: (Int) => Int = <function1>

scala> bar(5)
res2: Int = 10

您会看到,该函数没有名称 foo,它可以从 bar 调用。

其次,闭包是一个匿名函数,它有一个未在函数内部定义的变量(变量/值必须在定义函数之前声明)。术语“全动力闭合”可能指的是这种功能。

scala> var constant = 7
constant: Int = 7

scala> val foo = (x: Int) => 2*x + constant
foo: (Int) => Int = <function1>

scala> foo(5)
res3: Int = 17

scala> constant = 6
constant: Int = 6

scala> foo(5)
res4: Int = 16

第一次看到这个,你可能想知道它有什么用处。简而言之,它有很多应用领域:-)

Since I feel like a beginner (compared to DPP and Amber) I might explain it to a beginner in a beginners language:

First, anonymous function (or code block/lambda expression) is simply a function that does not have a name. It could be tied to a variable like this.

scala> val foo = (x: Int) => 2*x 
foo: (Int) => Int = <function1>

scala> val bar = foo
bar: (Int) => Int = <function1>

scala> bar(5)
res2: Int = 10

You see, the function doesn't have the name foo, it could be called from bar instead.

Second, a closure is an anonymous function that has a variable that is not defined inside the function (the variable/value must have been declared before the function is defined). The term "full powered closure" might refer to this functionality.

scala> var constant = 7
constant: Int = 7

scala> val foo = (x: Int) => 2*x + constant
foo: (Int) => Int = <function1>

scala> foo(5)
res3: Int = 17

scala> constant = 6
constant: Int = 6

scala> foo(5)
res4: Int = 16

First time you see this, you might wonder what it is good for. In short, it has many sectors of application :-)

献世佛 2024-09-16 21:01:45

说话者可能使用“全功率”来表示“类似闭合”的反义词,这就是许多语言实际上都有

It's possible that the speaker used 'full powered' to mean the opposite of 'closure-like', which is what many languages actually have.

影子是时光的心 2024-09-16 21:01:45

据我所知,全功能闭包并不意味着什么特别的,但它可能意味着:语法的简单性。在 Java 中新建匿名内部类与使用 (a => a < max) 之类的东西之间存在天壤之别。也许能够对所有周围变量(而不仅仅是最终变量)形成闭包。

As far as I know, full powered closures doesn't mean anything in particular but it may mean: simplicity of syntax. There is a world of difference between newing up an anonymous inner class in Java vs. using something like (a => a < max). Perhaps the ability to form closures on all surrounding variables not just final ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文