1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/math/mean.go
Tim Voronov 1af8b37a0f
New type system (#232)
* New type system

* Fixed dot notation for HTML elements
2019-02-13 12:31:18 -05:00

37 lines
646 B
Go

package math
import (
"math"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
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 {
err = core.ValidateType(value, types.Int, types.Float)
if err != nil {
return false
}
sum += toFloat(value)
return true
})
if err != nil {
return 0, err
}
return values.NewFloat(sum / float64(input.Length())), nil
}