Commutative Semigroup

CSemigroup[A] is a semigroup that is commutative.

In addition to the laws of a semigroup:

the following law must hold:

Code

trait CSemigroup[A] extends Semigroup[A]

Laws

Commutativity means that the order of elements when combining does not matter. In other words, the result of the combine operation will be the same regardless of the order in which you pass elements x and y. This can be written as: cs.combine(x, y) == cs.combine(y, x)

Diagram

classDiagram
    class Semigroup~A~{
        +combine(x: A, y: A) A
    }
    class CommutativeSemigroup~A~
    Semigroup <|-- CommutativeSemigroup  

Implementation in Libraries

Implementation in Spire

import spire.algebra.CSemigroup

CSemigroup.combine(1, 2) == CSemigroup.combine(2, 1)

References: