Skip to content

Latest commit

 

History

History
29 lines (19 loc) · 602 Bytes

semigroup-commutative.md

File metadata and controls

29 lines (19 loc) · 602 Bytes

Question

Can you find a Semigroup example where concat is commutative and one where it isn't?

Answer

Commutative:

import { Semigroup } from 'fp-ts/Semigroup'

const SemigroupSum: Semigroup<number> = {
  concat: (first, second) => first + second
}

concat(a, b) = a + b = b + a = concat(b, a) as the addition is commutative

Not commutative:

import { Semigroup } from 'fp-ts/Semigroup'

const first = <A>(): Semigroup<A> => ({
  concat: (first, _second) => first
})

concat(a, b) = a != concat(b, a)