且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-29 14:09:04

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

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).