2018-10-14 03:07:28 +02:00
|
|
|
package math
|
|
|
|
|
|
|
|
import (
|
2019-02-13 19:31:18 +02:00
|
|
|
"math"
|
|
|
|
|
2018-10-14 03:07:28 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-14 03:07:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func mean(input *values.Array) (values.Float, error) {
|
|
|
|
if input.Length() == 0 {
|
|
|
|
return values.NewFloat(math.NaN()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var sum float64
|
|
|
|
|
|
|
|
input.ForEach(func(value core.Value, idx int) bool {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(value, types.Int, types.Float)
|
2018-10-14 03:07:28 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sum += toFloat(value)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewFloat(sum / float64(input.Length())), nil
|
|
|
|
}
|