Add MaxIndex function
All checks were successful
ci / deploy (push) Successful in 1m2s

This commit is contained in:
Leon Mika 2025-06-19 13:20:29 +02:00
parent c73e50a091
commit f285d1f8e0
2 changed files with 32 additions and 1 deletions

View file

@ -13,6 +13,19 @@ func Map[T, U any](ts []T, fn func(t T) U) (us []U) {
return us
}
// Map returns a new slice containing the elements of ts transformed by the passed in function.
func MapIndex[T, U any](ts []T, fn func(t T, index int) U) (us []U) {
if ts == nil {
return nil
}
us = make([]U, len(ts))
for i, t := range ts {
us[i] = fn(t, i)
}
return us
}
// Map returns a new slice containing the elements of ts transformed by the passed in function, which
// can either either a mapped value of U, or an error. If the mapping function returns an error, MapWithError
// will return nil and the returned error.