22 lines
432 B
Go
22 lines
432 B
Go
package slices
|
|
|
|
func Map[I, O any](slice []I, mapper func(I) O) []O {
|
|
ret := make([]O, 0, len(slice))
|
|
for _, v := range slice {
|
|
ret = append(ret, mapper(v))
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func MapError[I, O any](slice []I, mapper func(I) (O, error)) ([]O, error) {
|
|
ret := make([]O, 0, len(slice))
|
|
for _, old := range slice {
|
|
new, err := mapper(old)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ret = append(ret, new)
|
|
}
|
|
return ret, nil
|
|
}
|