logs-analyzer/signoz/pkg/query-service/utils/slices.go

30 lines
502 B
Go
Raw Normal View History

2024-09-02 22:47:30 +03:00
package utils
// Map as in map-reduce.
func MapSlice[Slice ~[]Elem, Elem any, Output any](
slice Slice, mapper func(Elem) Output,
) []Output {
result := []Output{}
for _, item := range slice {
mapped := mapper(item)
result = append(result, mapped)
}
return result
}
func FilterSlice[Slice ~[]Elem, Elem any](
slice Slice, filterFn func(Elem) bool,
) Slice {
result := Slice{}
for _, item := range slice {
if filterFn(item) {
result = append(result, item)
}
}
return result
}