Use generic function to compute maximum

This commit is contained in:
Holger Obermaier 2023-08-17 11:46:15 +02:00
parent ec86a83a27
commit ceff67085b

View File

@ -45,7 +45,6 @@ func sumfunc(args interface{}) (interface{}, error) {
} }
func minAnyType[T float64 | float32 | int | int32 | int64](values []T) (interface{}, error) { func minAnyType[T float64 | float32 | int | int32 | int64](values []T) (interface{}, error) {
fmt.Println(values)
if len(values) == 0 { if len(values) == 0 {
return 0.0, errors.New("min function requires at least one argument") return 0.0, errors.New("min function requires at least one argument")
} }
@ -114,18 +113,35 @@ func avgfunc(args interface{}) (interface{}, error) {
return 0.0, nil return 0.0, nil
} }
// Get the maximum value func maxAnyType[T float64 | float32 | int | int32 | int64](values []T) (interface{}, error) {
func maxfunc(args interface{}) (interface{}, error) { if len(values) == 0 {
s := 0.0 return 0.0, errors.New("max function requires at least one argument")
values, ok := args.([]float64) }
if ok { var maximum T = values[0]
for _, x := range values { for _, value := range values {
if x > s { if value > maximum {
s = x maximum = value
}
} }
} }
return s, nil return maximum, nil
}
// Get the maximum value
func maxfunc(args interface{}) (interface{}, error) {
switch values := args.(type) {
case []float64:
return maxAnyType(values)
case []float32:
return maxAnyType(values)
case []int:
return maxAnyType(values)
case []int64:
return maxAnyType(values)
case []int32:
return maxAnyType(values)
default:
return 0.0, errors.New("function 'max' only on list of values (float64, float32, int, int32, int64)")
}
} }
// Get the median value // Get the median value