mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 12:37:25 +01:00
Use generic function to simplify code
This commit is contained in:
parent
a1c2c3856d
commit
c3004f8c6d
@ -15,40 +15,29 @@ import (
|
|||||||
* Arithmetic functions on value arrays
|
* Arithmetic functions on value arrays
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
func sumAnyType[T float64 | float32 | int | int32 | int64](values []T) interface{} {
|
||||||
|
var sum T
|
||||||
|
for _, value := range values {
|
||||||
|
sum += value
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
// Sum up values
|
// Sum up values
|
||||||
func sumfunc(args interface{}) (interface{}, error) {
|
func sumfunc(args interface{}) (interface{}, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
switch values := args.(type) {
|
switch values := args.(type) {
|
||||||
case []float64:
|
case []float64:
|
||||||
var s float64 = 0.0
|
return sumAnyType(values), nil
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
case []float32:
|
case []float32:
|
||||||
var s float32 = 0.0
|
return sumAnyType(values), nil
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
case []int:
|
case []int:
|
||||||
var s int = 0
|
return sumAnyType(values), nil
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
case []int64:
|
case []int64:
|
||||||
var s int64 = 0
|
return sumAnyType(values), nil
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
case []int32:
|
case []int32:
|
||||||
var s int32 = 0
|
return sumAnyType(values), nil
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
default:
|
default:
|
||||||
err = errors.New("function 'sum' only on list of values (float64, float32, int, int32, int64)")
|
err = errors.New("function 'sum' only on list of values (float64, float32, int, int32, int64)")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user