MapErr applies f to each element of the input, returning the mapped result and a combined error of all returned errors. Map uses up to the configured Mapper's maximum number of goroutines.
(input []T, f func(*T) (R, error))
| 46 | // |
| 47 | // Map uses up to the configured Mapper's maximum number of goroutines. |
| 48 | func (m Mapper[T, R]) MapErr(input []T, f func(*T) (R, error)) ([]R, error) { |
| 49 | var ( |
| 50 | res = make([]R, len(input)) |
| 51 | errMux sync.Mutex |
| 52 | errs error |
| 53 | ) |
| 54 | Iterator[T](m).ForEachIdx(input, func(i int, t *T) { |
| 55 | var err error |
| 56 | res[i], err = f(t) |
| 57 | if err != nil { |
| 58 | errMux.Lock() |
| 59 | // TODO: use stdlib errors once multierrors land in go 1.20 |
| 60 | errs = multierror.Join(errs, err) |
| 61 | errMux.Unlock() |
| 62 | } |
| 63 | }) |
| 64 | return res, errs |
| 65 | } |