MapError implemented
This commit is contained in:
parent
ab6e6684f8
commit
0592add8ca
12
map.go
12
map.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user