mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
3.2 KiB
3.2 KiB
name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | playUrl | variantHelpers | similarHelpers | position | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CrossJoinX | crossjoinx | it/tuples.go#L370 | it | tuple |
|
https://go.dev/play/p/4XrQKOk-vw |
|
|
10 |
Combines every item from multiple lists (cartesian product). The resulting sequence contains all possible combinations of elements from each input sequence.
The cartesian product means every element from the first sequence is paired with every element from the second sequence, then those pairs are combined with every element from the third sequence, and so on.
seq1 := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
}
seq2 := func(yield func(string) bool) {
_ = yield("a")
_ = yield("b")
}
cross := it.CrossJoin2(seq1, seq2)
var result []string
for tuple := range cross {
result = append(result, fmt.Sprintf("%d%s", tuple.A, tuple.B))
}
// result contains ["1a", "1b", "2a", "2b"] (all 4 combinations in lexical order)
Example with 3 sequences:
numbers := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
}
letters := func(yield func(string) bool) {
_ = yield("a")
_ = yield("b")
}
colors := func(yield func(string) bool) {
_ = yield("red")
_ = yield("blue")
}
cross := it.CrossJoin3(numbers, letters, colors)
var result []string
for tuple := range cross {
result = append(result, fmt.Sprintf("%d%s%s", tuple.A, tuple.B, tuple.C))
}
// result contains 8 combinations: ["1ared", "1ablue", "1bred", "1bblue", "2ared", "2ablue", "2bred", "2bblue"]