简体   繁体   English

为什么 Semigroup 有一个 Option 类型并且在 Monoid 中表现得像一个中性元素?

[英]Why does Semigroup has an Option type and behave like a neutral element in Monoid?

Why does Semigroup has an Option type and None behave like a neutral element in Monoid?为什么 Semigroup 有一个 Option 类型而 None 在 Monoid 中表现得像一个中性元素?

val two: Option[Int] = Option(2)
val n: Option[Int] = None

n |+| two should be(Some(2))//why do we have the opportunity to do this?
two |+| n should be(Some(2))//n look like neutral here

Semigroup[Option[Int]].combine(Option(1), None) should be(Some(1))//why does semigroup has it?

Every Monoid is also a Semigroup .每个Monoid也是一个Semigroup Semigroup doesn't have to have an identity element, but every Semigroup that is also a Monoid will always have one (without "knowing" about the concept). Semigroup不一定有单位元素,但每一个也是Monoid Semigroup总会有一个(不需要“知道”这个概念)。

Another example: "addition of integers" semigroup doesn't formally posses an identity element (zero).另一个例子:“整数加法”半群不正式拥有单位元素(零)。 It doesn't define one.它没有定义一个。 But you can still add 3 + 0 = 3.但是您仍然可以添加 3 + 0 = 3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM