slices/map.go

22 lines
432 B
Go
Raw Normal View History

2022-09-06 01:58:19 +02:00
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
}
2023-03-25 08:52:08 +01:00
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
}