github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/math/median.go (about) 1 package math 2 3 import ( 4 "context" 5 "math" 6 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 "github.com/MontFerret/ferret/pkg/runtime/values/types" 10 ) 11 12 // MEDIAN returns the median of the values in array. 13 // @param {Int[] | Float[]} array - Array of numbers. 14 // @return {Float} - The median of the values in array. 15 func Median(_ context.Context, args ...core.Value) (core.Value, error) { 16 err := core.ValidateArgs(args, 1, 1) 17 18 if err != nil { 19 return values.None, err 20 } 21 22 err = core.ValidateType(args[0], types.Array) 23 24 if err != nil { 25 return values.None, err 26 } 27 28 arr := args[0].(*values.Array) 29 sorted := arr.Sort() 30 31 l := sorted.Length() 32 33 var median core.Value 34 35 switch { 36 case l == 0: 37 return values.NewFloat(math.NaN()), nil 38 case l%2 == 0: 39 median, err = mean(sorted.Slice(l/2-1, l/2+1)) 40 41 if err != nil { 42 return values.None, nil 43 } 44 default: 45 median = sorted.Get(l / 2) 46 } 47 48 return median, nil 49 }