diff --git a/compliance/functions.json b/compliance/functions.json index 8b8db36..2f48184 100644 --- a/compliance/functions.json +++ b/compliance/functions.json @@ -63,6 +63,10 @@ "expression": "avg(numbers)", "result": 2.75 }, + { + "expression": "avg(empty_list)", + "result": null + }, { "expression": "avg(array)", "error": "invalid-type" diff --git a/functions.go b/functions.go index e9770e8..64eb2a9 100644 --- a/functions.go +++ b/functions.go @@ -428,6 +428,10 @@ func jpfAvg(arguments []interface{}) (interface{}, error) { // We've already type checked the value so we can safely use // type assertions. args := arguments[0].([]interface{}) + // An empty array is null. The zero sum divided by zero was NaN. + if len(args) == 0 { + return nil, nil + } length := float64(len(args)) numerator := 0.0 for _, n := range args {