diff --git a/utils/functional.go b/utils/functional.go new file mode 100644 index 0000000..c8f69fd --- /dev/null +++ b/utils/functional.go @@ -0,0 +1,27 @@ +package utils + +func Map[S, T any](source []S, f func(S) T) []T { + dest := make([]T, len(source)) + for i, elem := range source { + dest[i] = f(elem) + } + return dest +} + +func Reduce[S, T any](source []S, initialValue T, f func(T, S) T) T { + acc := initialValue + for _, elem := range source { + acc = f(acc, elem) + } + return acc +} + +func Filter[T any](source []T, f func(T) bool) []T { + var dest []T + for _, elem := range source { + if f(elem) { + dest = append(dest, elem) + } + } + return dest +}