Skip to content

Instantly share code, notes, and snippets.

@fogfish
Created March 27, 2022 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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