(name string, fn func(any, any) bool, depth int, args ...any)
| 234 | } |
| 235 | |
| 236 | func minMax(name string, fn func(any, any) bool, depth int, args ...any) (any, error) { |
| 237 | if depth > MaxDepth { |
| 238 | return nil, ErrorMaxDepth |
| 239 | } |
| 240 | var val any |
| 241 | for _, arg := range args { |
| 242 | // Fast paths for common typed slices - avoid reflection and allocations |
| 243 | switch arr := arg.(type) { |
| 244 | case []int: |
| 245 | if len(arr) == 0 { |
| 246 | continue |
| 247 | } |
| 248 | m := arr[0] |
| 249 | for i := 1; i < len(arr); i++ { |
| 250 | if fn(m, arr[i]) { |
| 251 | m = arr[i] |
| 252 | } |
| 253 | } |
| 254 | if val == nil || fn(val, m) { |
| 255 | val = m |
| 256 | } |
| 257 | continue |
| 258 | case []float64: |
| 259 | if len(arr) == 0 { |
| 260 | continue |
| 261 | } |
| 262 | m := arr[0] |
| 263 | for i := 1; i < len(arr); i++ { |
| 264 | if fn(m, arr[i]) { |
| 265 | m = arr[i] |
| 266 | } |
| 267 | } |
| 268 | if val == nil || fn(val, m) { |
| 269 | val = m |
| 270 | } |
| 271 | continue |
| 272 | case []any: |
| 273 | // Fast path for []any with simple numeric types |
| 274 | for _, elem := range arr { |
| 275 | switch e := elem.(type) { |
| 276 | case int, int8, int16, int32, int64, |
| 277 | uint, uint8, uint16, uint32, uint64, |
| 278 | float32, float64: |
| 279 | if val == nil || fn(val, e) { |
| 280 | val = e |
| 281 | } |
| 282 | case []int, []float64, []any: |
| 283 | // Nested array - recurse |
| 284 | nested, err := minMax(name, fn, depth+1, e) |
| 285 | if err != nil { |
| 286 | return nil, err |
| 287 | } |
| 288 | if nested != nil && (val == nil || fn(val, nested)) { |
| 289 | val = nested |
| 290 | } |
| 291 | default: |
| 292 | // Could be another slice type, use reflection |
| 293 | rv := reflect.ValueOf(e) |
no test coverage detected
searching dependent graphs…