This commit is contained in:
parent
c73e50a091
commit
f285d1f8e0
2 changed files with 32 additions and 1 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue