MCPcopy
hub / github.com/expr-lang/expr / mean

Function mean

builtin/lib.go:348–443  ·  view source on GitHub ↗
(depth int, args ...any)

Source from the content-addressed store, hash-verified

346}
347
348func mean(depth int, args ...any) (int, float64, error) {
349 if depth > MaxDepth {
350 return 0, 0, ErrorMaxDepth
351 }
352 var total float64
353 var count int
354
355 for _, arg := range args {
356 // Fast paths for common typed slices - avoid reflection and allocations
357 switch arr := arg.(type) {
358 case []int:
359 for _, v := range arr {
360 total += float64(v)
361 }
362 count += len(arr)
363 continue
364 case []float64:
365 for _, v := range arr {
366 total += v
367 }
368 count += len(arr)
369 continue
370 case []any:
371 // Fast path for []any - single pass without recursive calls for flat arrays
372 for _, elem := range arr {
373 switch e := elem.(type) {
374 case int:
375 total += float64(e)
376 count++
377 case float64:
378 total += e
379 count++
380 case []int, []float64, []any:
381 // Nested array - recurse
382 nestedCount, nestedSum, err := mean(depth+1, e)
383 if err != nil {
384 return 0, 0, err
385 }
386 total += nestedSum
387 count += nestedCount
388 default:
389 // Other numeric types or slices - use reflection
390 rv := reflect.ValueOf(e)
391 switch rv.Kind() {
392 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
393 total += float64(rv.Int())
394 count++
395 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
396 total += float64(rv.Uint())
397 count++
398 case reflect.Float32, reflect.Float64:
399 total += rv.Float()
400 count++
401 case reflect.Slice, reflect.Array:
402 nestedCount, nestedSum, err := mean(depth+1, e)
403 if err != nil {
404 return 0, 0, err
405 }

Callers 1

builtin.goFile · 0.85

Calls 2

ErrorfMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…