Back to Lo

Core Crossjoinbyerrx

docs/data/core-crossjoinbyerrx.md

1.53.0787 B
Original Source

Computes a cartesian product and projects each combination through a function that can return an error. Stops iteration immediately when an error is encountered and returns the zero value (nil for slices).

Variants: CrossJoinByErr2..CrossJoinByErr9

go
result, err := lo.CrossJoinByErr2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) (string, error) {
    if a == "b" {
        return "", fmt.Errorf("b not allowed")
    }
    return fmt.Sprintf("%s-%d", a, b), nil
})
// []string(nil), error("b not allowed")
go
result, err := lo.CrossJoinByErr2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) (string, error) {
    return fmt.Sprintf("%s-%d", a, b), nil
})
// []string{"a-1", "a-2", "b-1", "b-2"}, nil

Returns an empty list if any input list is empty.