Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CrossJoin function #567

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Supported helpers for slices:
- [IsSorted](#issorted)
- [IsSortedByKey](#issortedbykey)
- [Splice](#Splice)
- [CrossJoin](#CrossJoin)

Supported helpers for maps:

Expand Down Expand Up @@ -1039,7 +1040,20 @@ result = lo.Splice([]string{"a", "b"}, 42, "1", "2")
// []string{"a", "b", "1", "2"}
```

[[play](https://go.dev/play/p/G5_GhkeSUBA)]
[[play](https://go.dev/play/p/wiG6XyBBu49)]

### CrossJoin

CrossJoin calculates the cartesian product of two lists. It returns a list of tuples where the first element includes the elements of the first parameter, and the second element contains the elements of the second parameter.

It returns an empty list if either, or both parameters are empty

```go
result := lo.CrossJoin([]string{"a", "b", "c"}, []int{1, 2, 3})
// [][2]interface{}{{"a", 1}, {"a", 2}, {"a", 3}, {"b", 1}, {"b", 2}, {"b", 3}, {"c", 1}, {"c", 2}, {"c", 3}
```

[[play](https://go.dev/play/p/2-DOGciKvAB)]

### Keys

Expand Down
24 changes: 24 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,27 @@ func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice {

return append(append(append(output, collection[:i]...), elements...), collection[i:]...)
}

// CrossJoin calculates the cartesian product of two lists. It returns a list of
// tuples where the first element includes the elements of the first parameter, and
// the second element contains the elements of the second parameter.

// It returns an empty list if either, or both parameters are empty

// Play: https://go.dev/play/p/2-DOGciKvAB
func CrossJoin[T, U any](listOne []T, listTwo []U) [][2]interface{} {

if len(listOne) == 0 || len(listTwo) == 0 {
return make([][2]interface{}, 0)
}

cartesianProduct := make([][2]interface{}, 0, len(listOne)*len(listTwo))

for _, a := range listOne {
for _, b := range listTwo {
cartesianProduct = append(cartesianProduct, [2]interface{}{a, b})
}
}

return cartesianProduct
}
31 changes: 31 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,34 @@ func TestSplice(t *testing.T) {
nonempty := Splice(allStrings, 1, "1", "2")
is.IsType(nonempty, allStrings, "type preserved")
}

func TestCrossJoin(t *testing.T) {
t.Parallel()
is := assert.New(t)

listOne := []string{"a", "b", "c"}
listTwo := []int{1, 2, 3}
emptyList := make([][2]any, 0)
mixedList := []any{9.6, 4, "foobar"}

results := CrossJoin(emptyList, listTwo)
is.Equal(emptyList, results)

results = CrossJoin(listOne, emptyList)
is.Equal(emptyList, results)

results = CrossJoin(emptyList, emptyList)
is.Equal(emptyList, results)

results = CrossJoin([]string{"a"}, listTwo)
is.Equal([][2]any{{"a", 1}, {"a", 2}, {"a", 3}}, results)

results = CrossJoin(listOne, []int{1})
is.Equal([][2]any{{"a", 1}, {"b", 1}, {"c", 1}}, results)

results = CrossJoin(listOne, listTwo)
is.Equal([][2]any{{"a", 1}, {"a", 2}, {"a", 3}, {"b", 1}, {"b", 2}, {"b", 3}, {"c", 1}, {"c", 2}, {"c", 3}}, results)

results = CrossJoin(listOne, mixedList)
is.Equal([][2]any{{"a", 9.6}, {"a", 4}, {"a", "foobar"}, {"b", 9.6}, {"b", 4}, {"b", "foobar"}, {"c", 9.6}, {"c", 4}, {"c", "foobar"}}, results)
}