Why does Semigroup has an Option type and None behave like a neutral element in 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 aSemigroup
.Semigroup
doesn't have to have an identity element, but everySemigroup
that is also aMonoid
will always have one (without "knowing" about the concept).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.