Skip to content

Instantly share code, notes, and snippets.

@fogfish
Created March 27, 2022 19:14
Show Gist options
  • Save fogfish/df9ab04dc86bb270633ab8f76aa74a8c to your computer and use it in GitHub Desktop.
Save fogfish/df9ab04dc86bb270633ab8f76aa74a8c to your computer and use it in GitHub Desktop.
type Foldable[T any] interface {
Fold(T, []T) T
}
type Semigroup[T any] interface {
Combine(T, T) T
}
type Folder[T any] struct{ Semigroup[T] }
func (f Folder[T]) Fold(a T, seq []T) (x T) {
x = a
for _, y := range seq {
x = f.Semigroup.Combine(x, y)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment