Scala Cats 文档:Monoid[Pair[A,B]]

Scala Cats documentation: Monoid[Pair[A,B]]

我已经离开 Scala 好几年了(一开始从来没有真正适应过它),而是使用(对我来说)更容易学习和使用的语言。

我现在回来重新学习它并试验一些可用的库。我最近开始研究 Cats,但 运行 与文档中早期的一些内容发生冲突,这让我感到困惑,想知道我是否(再一次)错过了该语言的一些基本知识。

page describing typeclasses 上,我们看到以下内容:

For another motivating difference, consider the simple pair type.

final case class Pair[A, B](first: A, second: B)

Defining a Monoid[Pair[A, B]] depends on the ability to define a Monoid[A] and Monoid[B], where the definition is point-wise.

首先不知道作者"the definition is point-wise."

是什么意思

其次,我不知道为什么定义 Monoid[Pair[A, B]] 必须依赖于定义 Monoid[A]Monoid[B](并且从未解释过)。 Monoid 特征中似乎没有任何东西需要它。

这基本上与 Scala 或任何编程语言无关。作者只是简单地使用 monoids 作为一个(大概是众所周知的)例子来演示框架的一些特性。

平均 reader 的 Cats 文档对幺半群有点熟悉的假设并不太牵强,因为:

完整的句子是

Defining a Monoid[Pair[A, B]] depends on the ability to define a Monoid[A] and Monoid[B], where the definition is point-wise.

这句话以monoids为例,是为了说明一些关于类型类的东西。它大概想说的是:

Let (A, op_A, 1_A) and (B, op_B, 1_B) be two monoids. Then the cartesian product A x B can also be endowed with a structure of a monoid in a natural way as follows:

                     1_{AxB} := (1_A, 1_B)
  (a1, b1) op_{AxB} (a2, b2) := (a1 op_A a2, b1 op_B b2)

这句话说定义是“pointwise". I think the word "componentwise" would have been much more appropriate. Wikipedia article uses "pairwise" 无论出于何种原因。我更喜欢 "componentwise" 而不是 "pointwise" 和 "pairwise" 在这里。