我对以下 Scala 代码的理解是否正确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13052735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:39:55  来源:igfitidea点击:

Is my understanding of below scala code correct?

scala

提问by user701254

I'm just trying to understand the below code :

我只是想了解以下代码:



Here a new type alias Set is declared which is a function that takes an Int parameter and returns a boolean

这里声明了一个新的类型别名 Set,它是一个函数,它接受一个 Int 参数并返回一个布尔值

type Set = Int => Boolean


Here a new method 'contains' is declared, which takes two parameters of type Set and Int which returns a boolean. The boolean is set to the function declared in earlier ('type Set = Int => Boolean') But what logic is performed to determine if the Int 'elem' is a member of Set 's'

这里声明了一个新方法“contains”,它接受两个 Set 和 Int 类型的参数,返回一个布尔值。boolean 设置为之前声明的函数 ('type Set = Int => Boolean') 但是执行什么逻辑来确定 Int 'elem' 是否是 Set 's' 的成员

def contains(set: Set, elem: Int): Boolean = set(elem)


Here a method is defined which returns a set which returns a function ?

这里定义了一个方法,它返回一个返回一个函数的集合?

def singletonSet(elem: Int): Set = set => set == elem


Complete code with comments :

带注释的完整代码:

  /**
   * We represent a set by its characteristic function, i.e.
   * its `contains` predicate.
   */
  type Set = Int => Boolean

      /**
       * Indicates whether a set contains a given element.
       */
def contains(set: Set, elem: Int): Boolean = set(elem)

      /**
       * Returns the set of the one given element.
       */
      def singletonSet(elem: Int): Set = set => set == elem

回答by Paolo Falabella

Let's read sort of backwards, in logical order.

让我们按逻辑顺序倒退阅读。

Say you have a finite set of integers: 0, 1, 2, 3, 5, 8for instance

假设您有一组有限的整数:0, 1, 2, 3, 5, 8例如

One way to describe this set of integers is through a function (its characteristic or indicator function) that, for each integer, returns true if the integer is in the set, false if it is not. The signature for this function, as we described it, must always be Int => Boolean("give me an integer, I will tell you if it's in the set"), while its implementation will vary depending on the specific set.

描述这组整数的一种方法是通过一个函数(其特征或指示函数),对于每个整数,如果该整数在该集合中,则返回 true,否则返回 false。正如我们所描述的,这个函数的签名必须总是Int => Boolean(“给我一个整数,我会告诉你它是否在集合中”),而它的实现将根据特定的集合而变化。

For the set in my example above you could write this function simply as:

对于我上面例子中的集合,你可以简单地编写这个函数:

val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x

or recognize that the ints in the set are the first ones of the Fibonacci sequence and define f in a slightly more sophisticated way (which I won't do here...). Note that the "contains" I've used is defined for all scala collections. In any case, now you have a function that tells you what is in the set and what is not. Let's try it in the REPL.

或者认识到集合中的整数是斐波那契数列的第一个整数,并以稍微复杂的方式定义 f(我不会在这里做......)。请注意,我使用的“包含”是为所有 Scala 集合定义的。无论如何,现在您有一个函数可以告诉您集合中的内容和不包含的内容。让我们在 REPL 中尝试一下。

scala> val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>

scala> mySet(3)
res0: Boolean = true

scala> mySet(9)
res1: Boolean = false

Now, mySet has type Int => Boolean, which we can make more readable if we define it as a type alias.

现在, mySet 有 type Int => Boolean,如果我们将其定义为类型别名,我们可以使其更具可读性。

scala> type Set = Int => Boolean
defined type alias Set

Besides readability, defining Setas an alias of Int => Booleanis making it explicit that in a way a Set isits characteristic function. We can redefine mySet in a more concise (but otherwise equivalent) way with the Settype alias:

除了可读性之外,定义Set为的别名Int => Boolean可以明确地表明 Set它的特征函数。我们可以使用Set类型别名以更简洁(但等效)的方式重新定义 mySet :

scala> val mySet: Set = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>

Now for the last piece of this long answer. Let's define a characteristic function to describe this Singleton set: 3. Easy:

现在是这个长答案的最后一部分。让我们定义一个特征函数来描述此Singleton集:3。简单:

val Singleton3 : Set = set => set == 3

for a Singleton set containing only 4, it would be:

对于仅包含 4 个的单例集,它将是:

val Singleton4 : Set = set => set == 4

So, let's generalize the creation of these functions and write a method that returns a Singleton function that, for any integer, describes the set containing only that integer:

因此,让我们概括这些函数的创建并编写一个方法,该方法返回一个 Singleton 函数,该函数对于任何整数,描述仅包含该整数的集合:

def singletonSet(elem: Int): Set = set => set == elem


APPENDIX:

附录:

I skipped this part, because it wasn't really needed: def contains(set: Set, elem: Int): Boolean = set(elem)

我跳过了这部分,因为它不是真的需要: def contains(set: Set, elem: Int): Boolean = set(elem)

I think it's sort of pointless and (without more context) it looks just like a contrived example to demonstrate how you can pass a function around as an argument, just like any other type in scala. It takes the Int => Boolfunction and the Intand just applies the function to the Intso you can do

我认为这有点毫无意义并且(没有更多上下文)它看起来就像一个人为的例子来演示如何将函数作为参数传递,就像 scala 中的任何其他类型一样。它需要Int => Bool函数和 theInt并且只是将函数应用于 theInt所以你可以做

scala> contains(mySet, 3)
res2: Boolean = true

which is like calling mySet(3)directly.

这就像mySet(3)直接打电话一样。

回答by witchking

After watching the lecture video on "Currying", I believe that Paolo's solution expressed in a more verbose manner is :

看了“柯里化”的讲座视频,相信Paolo用更详细的方式表达的解决方案是:

    def singletonSet(elem: Int): Set = {
    def innerFunction (givenElement: Int) = 
      if (elem == givenElement) true
      else false
      innerFunction
  }

Plesae correct me if I am wrong!

如果我错了,请纠正我!

回答by Ramya

To answer your question - But what logic is performed to determine if the Int 'elem' is a member of Set 's'

回答你的问题 -但是执行什么逻辑来确定 Int 'elem' 是否是 Set 's' 的成员

This is performed when you make the actual function call. Consider the following function call.

这是在您进行实际函数调用时执行的。考虑以下函数调用。

contains(singletonSet(1), 1)

包含(singletonSet(1), 1)

Now singletonSet is defined as def singletonSet(elem: Int): Set = x => x == elem(I choose to use the identifier x for clarity sake). The return type of the singletonSet is the function of type Set which takes an Int argument and returns Boolean. So the above calling function's first argument singletonSet(1) equates to the function x => x == 1as elem here is 1. So we get

现在 singletonSet 被定义为def singletonSet(elem: Int): Set = x => x == elem(为了清楚起见,我选择使用标识符 x)。singletonSet 的返回类型是 Set 类型的函数,它接受一个 Int 参数并返回 Boolean。所以上面调用函数的第一个参数 singletonSet(1) 等同于函数x => x == 1因为这里的 elem 是 1. 所以我们得到

contains((x => x == 1),1)

包含((x => x == 1),1)

Now considering the definition of contains function def contains(f: Set, elem: Int): Boolean = f(elem). The first argument in the call above is the function x => x == 1 which substitutes formal parameter f and second argument 1 substitutes formal parameter elem. The return value of contains is the function f(elem) which equates to f(1). Since f(x) is defined as (x == 1), f(1) equates to (1 == 1) which returns true.

现在考虑 contains 函数def contains(f: Set, elem: Int): Boolean = f(elem) 的定义。上面调用中的第一个参数是函数 x => x == 1,它替代了形式参数 f,第二个参数 1 替代了形式参数 elem。contains 的返回值是等于 f(1) 的函数 f(elem)。由于 f(x) 被定义为 (x == 1),因此 f(1) 等于 (1 == 1),它返回 true。

Going by the same logic, a function call like contains(singletonSet(1), 2) would finally equate to (1 == 2) which will return false.

按照相同的逻辑,像 contains(singletonSet(1), 2) 这样的函数调用最终将等同于 (1 == 2) 这将返回 false。

回答by JohnnyHuo

I'm taking the course now, was also confused, but I think I get the point now.

我现在正在参加课程,也很困惑,但我想我现在明白了。

def singletonSet(elem: Int): Set = (x : Int) => x == elem

def singletonSet(elem: Int): Set = (x : Int) => x == elem

here singletonSet is a function that returns a function with the type Set, which is defined as type Set = Int => Boolean

这里的 singletonSet 是一个函数,它返回一个类型为 Set 的函数,定义为type Set = Int => Boolean

so when you call def contains(s:Set, elem:Int): Boolean = s(elem), for example: contains(singletonSet(1), 2), singletonSet(1) is returning a function with elem(in the definition of singletonSet) set to 1, and the 2(also defined as elem, but is defined in the parameter of contains) is passed in as the x in singletonSet definition, we need to get rid of java set idea, we don't need the singletonSet to persist the value we set.

所以当你调用时def contains(s:Set, elem:Int): Boolean = s(elem),例如:contains(singletonSet(1), 2),singletonSet(1) 正在返回一个 elem(在 singletonSet 的定义中)设置为 1 的函数,并且传递了 2(也定义为 elem,但在 contains 的参数中定义)在singletonSet定义中的x,我们需要摆脱java set的想法,我们不需要singletonSet来持久化我们设置的值。

For better understanding, we can change the parameter name as following:

为了更好地理解,我们可以将参数名称更改如下:

def singletonSet(elemToStore: Int): Set = (x : Int) => x == elemToStore

def singletonSet(elemToStore: Int): Set = (x : Int) => x == elemToStore

def contains(s: Set, elemToCheck: Int): Boolean = s(elemToCheck)

def contains(s: Set, elemToCheck: Int): Boolean = s(elemToCheck)