MCPcopy Index your code
hub / github.com/expr-lang/expr / median

Function median

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

Source from the content-addressed store, hash-verified

443}
444
445func median(depth int, args ...any) ([]float64, error) {
446 if depth > MaxDepth {
447 return nil, ErrorMaxDepth
448 }
449 var values []float64
450
451 for _, arg := range args {
452 // Fast paths for common typed slices - avoid reflection and allocations
453 switch arr := arg.(type) {
454 case []int:
455 for _, v := range arr {
456 values = append(values, float64(v))
457 }
458 continue
459 case []float64:
460 values = append(values, arr...)
461 continue
462 case []any:
463 // Fast path for []any - single pass without recursive calls for flat arrays
464 for _, elem := range arr {
465 switch e := elem.(type) {
466 case int:
467 values = append(values, float64(e))
468 case float64:
469 values = append(values, e)
470 case []int, []float64, []any:
471 // Nested array - recurse
472 elems, err := median(depth+1, e)
473 if err != nil {
474 return nil, err
475 }
476 values = append(values, elems...)
477 default:
478 // Other numeric types or slices - use reflection
479 rv := reflect.ValueOf(e)
480 switch rv.Kind() {
481 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
482 values = append(values, float64(rv.Int()))
483 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
484 values = append(values, float64(rv.Uint()))
485 case reflect.Float32, reflect.Float64:
486 values = append(values, rv.Float())
487 case reflect.Slice, reflect.Array:
488 elems, err := median(depth+1, e)
489 if err != nil {
490 return nil, err
491 }
492 values = append(values, elems...)
493 default:
494 return nil, fmt.Errorf("invalid argument for median (type %T)", e)
495 }
496 }
497 }
498 continue
499 }
500
501 // Slow path: use reflection for other types
502 rv := reflect.ValueOf(arg)

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…