-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
join.go
38 lines (32 loc) · 1.21 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package underscore
// Join joins two slices together and returns a Tuple of [T, []P], the selectors allow you to pick the
// keys you want to use from your struct's to join the sets together
func Join[T, P any, S comparable](
left []T,
right []P,
leftSelector func(T) S,
rightSelector func(P) S) []Tuple[T, []P] {
var results = make([]Tuple[T, []P], 0, len(left))
for _, l := range left {
var matches = Filter(right, func(r P) bool { return leftSelector(l) == rightSelector(r) })
var tuple = Tuple[T, []P]{Left: l, Right: matches}
results = append(results, tuple)
}
return results
}
// JoinProject joins two slices together and returns a []O where O is defined by the output
// of your projection function
// The selectors allow you to pick the keys from your structure to use as the join keys
// While the projection functions allows you to reformat joined datasets
// (Tuple of [T, []P]) into your own struct or type
func JoinProject[L, R, O any, S comparable](
left []L,
right []R,
leftSelector func(L) S,
rightSelector func(R) S,
projection func(Tuple[L, []R]) O) (results []O) {
for _, x := range Join(left, right, leftSelector, rightSelector) {
results = append(results, projection(x))
}
return results
}