Files
lo/docs/data/core-productbyerr.md
T
2026-03-02 16:06:51 +01:00

1.1 KiB

name, slug, sourceRef, category, subCategory, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory variantHelpers similarHelpers position signatures
ProductByErr productbyerr math.go#L134 core math
core#math#productbyerr
core#math#productby
core#math#product
core#math#sumbyerr
core#math#meanby
core#find#minby
core#find#maxby
71
func ProductByErr[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) (R, error)) (R, error)

Calculates the product of values computed by a predicate. Returns 1 for nil or empty collections.

If the iteratee returns an error, iteration stops and the error is returned.

strings := []string{"foo", "bar"}
result, err := lo.ProductByErr(strings, func(item string) (int, error) {
    return len(item), nil
})
// 9, <nil>

Example with error:

strings := []string{"foo", "bar", "baz"}
result, err := lo.ProductByErr(strings, func(item string) (int, error) {
    if item == "bar" {
        return 0, fmt.Errorf("bar is not allowed")
    }
    return len(item), nil
})
// 3, error("bar is not allowed")