1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-06-23 00:27:49 +02:00
Files
fp-go/monoid/monoid.go
Dr. Carsten Leue c07df5c771 initial checkin
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-07 22:31:06 +02:00

34 lines
653 B
Go

package monoid
import (
S "github.com/ibm/fp-go/semigroup"
)
type Monoid[A any] interface {
S.Semigroup[A]
Empty() A
}
type monoid[A any] struct {
c func(A, A) A
e A
}
func (self monoid[A]) Concat(x A, y A) A {
return self.c(x, y)
}
func (self monoid[A]) Empty() A {
return self.e
}
// MakeMonoid creates a monoid given a concat function and an empty element
func MakeMonoid[A any](c func(A, A) A, e A) Monoid[A] {
return monoid[A]{c: c, e: e}
}
// Reverse returns the dual of a `Monoid`, obtained by swapping the arguments of `Concat`.
func Reverse[A any](m Monoid[A]) Monoid[A] {
return MakeMonoid(S.Reverse[A](m).Concat, m.Empty())
}