我对以下 Scala 代码的理解是否正确? [英] Is my understanding of below scala code correct?

查看:41
本文介绍了我对以下 Scala 代码的理解是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解以下代码:

I'm just trying to understand the below code :

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

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

type Set = Int => Boolean

<小时>

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


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'

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

推荐答案

让我们按逻辑顺序倒序阅读.

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

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

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

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

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.

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

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

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

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.

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

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

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

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

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

Besides readability, defining Set as an alias of Int => Boolean is making it explicit that in a way a Set is its characteristic function. We can redefine mySet in a more concise (but otherwise equivalent) way with the Set type alias:

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

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

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

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

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

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

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:

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

<小时>

附录:

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

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

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

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 => Bool function and the Int and just applies the function to the Int so you can do

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

这就像直接调用 mySet(3).

这篇关于我对以下 Scala 代码的理解是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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