underscore/max.go
Ruidy a1943556b4
fix: add explicit panic for Max/Min on empty slices
Adds length checks to Max and Min functions to provide clear,
explicit panic messages when called with empty slices, rather
than allowing confusing index-out-of-bounds panics.

Changes:
- Max: Added check for empty slice with "underscore.Max: empty slice" panic
- Min: Added check for empty slice with "underscore.Min: empty slice" panic
- Updated doc comments to document panic behavior
- Added TestMaxEmpty and TestMinEmpty to verify panic behavior

Impact:
- Better error messages for debugging
- Documented behavior prevents user surprises
- Non-breaking change (still panics, just with clearer message)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 13:51:35 +01:00

20 lines
405 B
Go

package underscore
import "cmp"
// Max returns the maximum value in the slice.
// Panics if values is empty.
// This function can currently only compare numbers reliably.
// This function uses operator <.
func Max[T cmp.Ordered](values []T) T {
if len(values) == 0 {
panic("underscore.Max: empty slice")
}
max := values[0]
for _, v := range values {
if v > max {
max = v
}
}
return max
}