MapError implemented

This commit is contained in:
milarin 2023-03-25 08:52:08 +01:00
parent ab6e6684f8
commit 0592add8ca
1 changed files with 12 additions and 0 deletions

12
map.go
View File

@ -7,3 +7,15 @@ func Map[I, O any](slice []I, mapper func(I) O) []O {
}
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
}