Scala关闭的问题(来自“Scala中的编程”) [英] Question on Scala Closure (From "Programming in Scala")

查看:95
本文介绍了Scala关闭的问题(来自“Scala中的编程”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么作者说代码9.1从Scala编程中使用闭包。在第9章中,它们展示了如何将代码重构为更少的重复形式,从这个原始代码:

I don't understand why authors said that Code Listing 9.1 from "Programming in Scala" use closure. In chapter 9, they show how to refactor code into more less duplicated form, from this original code:

object FileMatcher {
  private def filesHere = (new java.io.File(".")).listFiles
  def filesEnding(query: String) =
    for (file <- filesHere; if file.getName.endsWith(query))
      yield file
  def filesContaining(query: String) =
    for (file <- filesHere; if file.getName.contains(query))
      yield file
  def filesRegex(query: String) =
    for (file <- filesHere; if file.getName.matches(query))
      yield file
}

到第二个版本:

object FileMatcher {
  private def filesHere = (new java.io.File(".")).listFiles
  def filesMatching(query: String,
    matcher: (String, String) => Boolean) = {
      for (file <- filesHere; if matcher(file.getName, query))
        yield file
    }    
  def filesEnding(query: String) =
    filesMatching(query, _.endsWith(_))
  def filesContaining(query: String) =
    filesMatching(query, _.contains(_))
  def filesRegex(query: String) =
    filesMatching(query, _.matches(_))
}

他们说这里没有使用closure。现在我明白了,直到这一点。但是,他们引入了使用 closure 来重构更多内容,如清单9.1所示:

Which they said that there is no use of closure here. Now I understand until this point. However they introduced the use of closure to refactor even some more, shown in Listing 9.1:

object FileMatcher {
  private def filesHere = (new java.io.File(".")).listFiles
  private def filesMatching(matcher: String => Boolean) =
    for (file <- filesHere; if matcher(file.getName))
      yield file
  def filesEnding(query: String) =
    filesMatching(_.endsWith(query))
  def filesContaining(query: String) =
    filesMatching(_.contains(query))
  def filesRegex(query: String) =
    filesMatching(_.matches(query))
}

现在他们说查询是一个自由变量,他们为什么这么说?因为查询似乎从顶部方法传递到字符串匹配函数显式。

Now they said that query is a free variable but I don't really understand why they said so? Since ""query"" seems to be passed from top method down to string matching function explicitly.

推荐答案

让我们看看经典添加关闭从什么是关闭

Let's look at the classic add-n closure from What is a closure.

(define (add a)
  (lambda (b)
    (+ a b)))

(define add3 (add 3))

(add3 4) returns 7

以上lambda表达式, a 自由变量< a>,在维基百科链接中定义为:

In the above lambda expression, a is the free variable, which is defined in the Wikipedia link to be:


在函数
中引用的变量不是局部变量或
参数。上限
是一个已经用闭包绑定
(已结束)的自由变量。

a variable referred to in a function that is not a local variable or an argument of that function. An upvalue is a free variable that has been bound (closed over) with a closure.

回到

def filesEnding(query: String) =
  filesMatching(_.endsWith(query))

隐式函数 x => x.endsWith(query)是第一类函数,它被分配给第一类值 matcher _.endsWith()查询上关闭,类似于3关闭 a in (add 3)(add3 4)等效由 matcher(file.getName)

The implicit function x => x.endsWith(query) is the first-class function, which is assigned to first-class value matcher, and _.endsWith() is closed over query, similar to the way 3 closes up a in (add 3). (add3 4) equivalent is done by matcher(file.getName).

编辑:Tricky部分是Scala中称为占位符语法匿名函数的功能。通过使用 _ 代替发送者或参数,Scala自动创建一个匿名函数,我们可以将其视为lambda表达式。

Edit: Tricky part is the feature in Scala called placeholder syntax anonymous functions. By using _ in place of the sender or the parameter, Scala automatically creates an anonymous function, which we can consider it as lambda expression.

例如,

_ + 1              creates       x => x + 1
_ * _              creates       (x1, x2) => x1 * x2
_.endsWith(query)  creates       x => x.endsWith(query)

x.endsWith(query),查询符合作为自由变量的两个要求:

Within the function x => x.endsWith(query), query meets the two requirements of being a free variable:


  1. 查询不是在函数内定义的局部变量(没有局部变量)。

  2. 查询不是函数的参数(唯一的参数是 x )。

  1. query is not a local variable defined within the function (there is no local variable).
  2. query is not an argument of the function (the only argument is x).

这篇关于Scala关闭的问题(来自“Scala中的编程”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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