-
Notifications
You must be signed in to change notification settings - Fork 1
/
Funky.go
81 lines (64 loc) · 1.58 KB
/
Funky.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package MultiElo
import (
u "github.com/rjNemo/underscore"
"golang.org/x/exp/constraints"
)
type Tuple[L, R any] struct {
Left L
Right R
}
func Range(start int, end int) (result []int) {
for i := start; i <= end; i++ {
result = append(result, i)
}
return result
}
func Zip[L any, R any](left []L, right []R) []Tuple[L, R] {
var shortest = 0
if len(left) < len(right) {
shortest = len(left)
} else {
shortest = len(right)
}
results := make([]Tuple[L, R], shortest)
for i := 0; i < shortest; i++ {
results[i] = Tuple[L, R]{Left: left[i], Right: right[i]}
}
return results
}
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 = u.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
}
func JoinProject[T, P, R any, S comparable](
left []T,
right []P,
leftSelector func(T) S,
rightSelector func(P) S,
projection func(Tuple[T, []P]) R) (results []R) {
for _, x := range Join(left, right, leftSelector, rightSelector) {
results = append(results, projection(x))
}
return results
}
func Sum[T any, R constraints.Ordered](list []T, selector func(T) R) (sum R) {
for _, v := range list {
sum += selector(v)
}
return sum
}
func DiagonalFill[T constraints.Ordered](list [][]T, f func(T) T) [][]T {
for i := 0; i < len(list); i++ {
list[i][i] = f(list[i][i])
}
return list
}