File size: 887 Bytes
6380833 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package slicesx
import (
"errors"
)
// Map maps elements of a slice from T to M, returning a new slice.
func Map[T any, S any](s []T, f func(T) S) []S {
// Nil input, return early.
if s == nil {
return nil
}
n := make([]S, len(s))
for i, v := range s {
n[i] = f(v)
}
return n
}
// MapWithErr maps elements of a slice from T to M, returning a new slice and a joined error if there are any.
// If an error is returned from the mapping function, a nil array and the error is returned.
func MapWithErr[T any, S any](s []T, f func(T) (S, error)) ([]S, error) {
// Nil input, return early.
if s == nil {
return nil, nil
}
var outErr error
n := make([]S, 0, len(s))
for _, v := range s {
res, err := f(v)
if err != nil {
outErr = errors.Join(outErr, err)
continue
}
n = append(n, res)
}
if outErr != nil {
return nil, outErr
}
return n, nil
}
|