Created
March 27, 2022 19:14
-
-
Save fogfish/df9ab04dc86bb270633ab8f76aa74a8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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