docs/data/core-reducerighterr.md
Like Reduce but iterates from right to left and accumulates into a single value using an accumulator function that can return an error. Stops iteration immediately when an error is encountered.
// Error case - stops on first error (from right to left)
result, err := lo.ReduceRightErr([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg []int, item []int, _ int) ([]int, error) {
if len(item) > 0 && item[0] == 4 {
return nil, fmt.Errorf("element starting with 4 is not allowed")
}
return append(agg, item...), nil
}, []int{})
// []int(nil), error("element starting with 4 is not allowed")
// Success case
result, err := lo.ReduceRightErr([]int{1, 2, 3, 4}, func(agg int, item int, _ int) (int, error) {
return agg + item, nil
}, 0)
// 10, nil