我应该如何看待 Scala 的 Product 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1301907/
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 01:36:08  来源:igfitidea点击:

How should I think about Scala's Product classes?

scalafunctional-programming

提问by

The package "scala" has a number of classes named Product, Product1, Product2, and so on, up to Product22.

包“scala”有许多类,名为 Product、Product1、Product2 等等,直到 Product22。

The descriptions of these classes are surely precise. For example:

这些类的描述肯定是准确的。例如:

Product4 is a cartesian product of 4 components

Precise, yes. Communicative? Not so much. I expect that this is the perfect wording for someone who already understands the sense of "cartesian product" being used here. For someone who doesn't, it sounds a bit circular. "Oh yes, well of course Product4 is the mumbleproduct of 4 mumble-mumbles."

准确的,是的。沟通?没那么多。我希望对于已经理解此处使用的“笛卡尔积”含义的人来说,这是一个完美的措辞。对于不知道的人来说,这听起来有点循环。“哦,是的,还有当然产品4是喃喃的4个产品咕哝,喃喃自语。”

Please help me understand the correct functional-language viewpoint. What is the sense of "cartesian product" being used here? What do the Product classes' "projection" members indicate?

请帮助我理解正确的函数式语言观点。这里使用的“笛卡尔积”是什么意思?Product 类的“投影”成员表示什么?

采纳答案by Daniel C. Sobral

"The set of all possible pairs of elements whose components are members of two sets."

“所有可能的元素对的集合,其组件是两个集合的成员。”

"Specifically, the Cartesian product of two sets X (for example the points on an x-axis) and Y (for example the points on a y-axis), denoted X × Y, is the set of all possible ordered pairs whose first component is a member of X and whose second component is a member of Y (e.g. the whole of the x-y plane)"

“具体地说,两个集合 X(例如 x 轴上的点)和 Y(例如 y 轴上的点)的笛卡尔积,表示为 X × Y,是所有可能的有序对的集合,它们的第一个组件是 X 的成员,其第二个组件是 Y 的成员(例如整个 xy 平面)"

Perhaps better understanding can be gained by knowing who derives from it:

也许通过了解谁从中获得更好的理解:

Direct Known Subclasses: Tuple4

直接已知子类:Tuple4

Or by, knowing it "extends Product", know what other classes can make use of it, by virtue of extending Productitself. I won't quote that here, though, because it's rather long.

或者,知道它“扩展了 Product”,知道其他类可以利用它,凭借扩展Product自身。不过,我不会在这里引用它,因为它很长。

Anyway, if you have types A, B, C and D, then Product4[A,B,C,D] is a class whose instances are all possible elements of the cartesian product of A, B, C and D. Literally.

无论如何,如果您有类型 A、B、C 和 D,那么 Product4[A,B,C,D] 是一个类,其实例都是 A、B、C 和 D 的笛卡尔积的所有可能元素。字面意思。

Except, of course, that Product4 is a Trait, not a class. It just provides a few useful methods for classes that are cartesian products of four different sets.

当然,除了 Product4 是一个 Trait,而不是一个类。它只是为四个不同集合的笛卡尔积的类提供了一些有用的方法。

回答by oxbow_lakes

Everyone else has gone for the maths so I'll go for the silly answer just in case! You have a simple car which has a gearbox, a steering wheel, an accelerator and a number of passengers. These can each vary: which gear are you in, which way are you steering, is your foot "on the floor"etc. The gearbox, steering, accelerator etc are therefore variablesand each has its own setof possible values.

其他人都去考数学了,所以我会去找愚蠢的答案以防万一!你有一辆简单的汽车,它有一个变速箱、一个方向盘、一个油门和一些乘客。它们各不相同:您在哪个档位,您转向哪种方式,您的脚是否“在地板上”等。因此,变速箱、转向、加速器等都是变量,每个都有自己的一可能值。

The cartesian product of each of these sets is basically all possible states that your car can be in. So a few possible values are:

这些集合中的每一个的笛卡尔积基本上是您的汽车可以处于的所有可能状态。所以一些可能的值是:

(gear,    steer,    accel,     pssngers)
--------|---------|----------|---------
(1st,     left,     foot down, none)
(neutral, straight, off,       the kids)

the size of the cartesian product is of course the product (multiplication) of the possibilities of each set. hence if you car has 5 gears (+ reverse + neutral), steering is left/straight/right, accelerator is on/off and up to 4 passengers, then there are 7 x 3 x 2 x 4 or 168 possible states.

笛卡尔积的大小当然是每个集合的可能性的乘积(乘法)。因此,如果您的汽车有 5 个档位(+ 倒档 + 空档),转向左/直/右,油门开/关且最多可容纳 4 名乘客,则有 7 x 3 x 2 x 4 或 168 种可能的状态。

This last fact is the reason that the cartesian product (named after Rene Descartesby the way) has the multiplication symbol x

最后一个事实是笛卡尔积(顺便提一下以勒内·笛卡尔命名)具有乘法符号的原因x

回答by VonC

From this thread:

这个线程

From mathematics, a Cartesian Product of two sets A, B is denoted as AxBand its elements are (a, b), where a in A and b in B.

For three sets, the elements of the (Cartesian) product are (a, b, c)and so on...

So, you have tuples of elements, and indeed you can see in the Scala library that all the tuples (like Tuple1) inherit the respective product trait (like Product1).

Think of product as the abstraction and the respective tuple a concrete representation.

从数学上讲,两个集合 A、B 的笛卡尔积表示为AxB,其元素为(a, b),其中 a 在 A 中,b 在 B 中。

对于三个集合,(笛卡尔)积的元素是(a, b, c)等等......

所以,你有元素的元组,实际上你可以在 Scala 库中看到所有的元组(比如Tuple1)都继承了各自的产品特征(比如Product1)。

将产品视为抽象,将相应的元组视为具体表示

The projection allows to get the instance of the 'n' class referenced by the Product.

投影允许获取产品引用的“n”类的实例。

回答by Thom Smith

A cartesian product is a product of sets. Given sets A and B, A x B ("A cross B") is the set of all tuples (x, y) such that x is in A and y is in B. A cartesian product may be analogously defined on types: given types A and B, A x B is the type of tuples (x, y) where x is of type A and y is of type B.

笛卡尔积是集合的积。给定集合 A 和 B,A x B(“A 交叉 B”)是所有元组 (x, y) 的集合,使得 x 在 A 中,y 在 B 中。笛卡尔积可以类似地定义在类型上:给定类型 A 和 B,A x B 是元组 (x, y) 的类型,其中 x 是 A 类型,y 是 B 类型。

So Product4 is the type of tuples (w, x, y, z), where w, x, y, z are components.

所以 Product4 是元组 (w, x, y, z) 的类型,其中 w, x, y, z 是组件。

回答by Norman Bai

I think somebody might feel confused for Productjust works like a member iterator, just like I did.

我认为有人可能会因为Product像成员迭代器一样工作而感到困惑,就像我一样。

In fact, I think in 2019 everybody knows what a Cartesian Productis. But where is Cartesian Product in a Tuple ? I know if we have {a,b,c} and {1,2,3} we'll get {a,1},{a,2}...{c,3}. But when we come across Tuple2(a,1) we just have (a,1), how can one object Product?

事实上,我认为在 2019 年每个人都知道笛卡尔积是什么。但是元组中的笛卡尔积在哪里?我知道如果我们有 {a,b,c} 和 {1,2,3},我们会得到 {a,1},{a,2}...{c,3}。但是当我们遇到 Tuple2(a,1) 时,我们只有 (a,1),一个产品怎么能对象呢?

So let's treat classes that implement Productas declarations. If class A(String, Int, Double) implentment Product3 , we treat the class as result of a Cartesian Productof (String, Int, Double), thus you know you can use _1_2_3method now.

因此,让我们将实现的类Product视为声明。如果类 A(String, Int, Double) 实现 Product3 ,我们将类视为(String, Int, Double)的笛卡尔积的结果,因此您知道现在可以使用_1_2_3方法了。