1
2
3
4
5 package a
6
7 type Ordered interface {
8 ~int | ~int8 | ~int16 | ~int32 | ~int64 |
9 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
10 ~float32 | ~float64 |
11 ~string
12 }
13
14
15 func Max[T Ordered](a, b T) T {
16 if a > b {
17 return a
18 }
19 return b
20 }
21
22
23 func Min[T Ordered](a, b T) T {
24 if a < b {
25 return a
26 }
27 return b
28 }
29
30
31
32 func Equal[Elem comparable](s1, s2 []Elem) bool {
33 if len(s1) != len(s2) {
34 return false
35 }
36 for i, v1 := range s1 {
37 v2 := s2[i]
38 if v1 != v2 {
39 isNaN := func(f Elem) bool { return f != f }
40 if !isNaN(v1) || !isNaN(v2) {
41 return false
42 }
43 }
44 }
45 return true
46 }
47
48
49
50 func EqualFn[Elem any](s1, s2 []Elem, eq func(Elem, Elem) bool) bool {
51 if len(s1) != len(s2) {
52 return false
53 }
54 for i, v1 := range s1 {
55 v2 := s2[i]
56 if !eq(v1, v2) {
57 return false
58 }
59 }
60 return true
61 }
62
63
64 func Map[Elem1, Elem2 any](s []Elem1, f func(Elem1) Elem2) []Elem2 {
65 r := make([]Elem2, len(s))
66 for i, v := range s {
67 r[i] = f(v)
68 }
69 return r
70 }
71
72
73
74 func Reduce[Elem1, Elem2 any](s []Elem1, initializer Elem2, f func(Elem2, Elem1) Elem2) Elem2 {
75 r := initializer
76 for _, v := range s {
77 r = f(r, v)
78 }
79 return r
80 }
81
82
83 func Filter[Elem any](s []Elem, f func(Elem) bool) []Elem {
84 var r []Elem
85 for _, v := range s {
86 if f(v) {
87 r = append(r, v)
88 }
89 }
90 return r
91 }
92
93
94
95 func SliceMax[Elem Ordered](s []Elem) Elem {
96 if len(s) == 0 {
97 var zero Elem
98 return zero
99 }
100 return Reduce(s[1:], s[0], Max[Elem])
101 }
102
103
104
105 func SliceMin[Elem Ordered](s []Elem) Elem {
106 if len(s) == 0 {
107 var zero Elem
108 return zero
109 }
110 return Reduce(s[1:], s[0], Min[Elem])
111 }
112
113
114
115
116
117
118 func Append[T any](s []T, t ...T) []T {
119 lens := len(s)
120 tot := lens + len(t)
121 if tot <= cap(s) {
122 s = s[:tot]
123 } else {
124 news := make([]T, tot, tot+tot/2)
125 Copy(news, s)
126 s = news
127 }
128 Copy(s[lens:tot], t)
129 return s
130 }
131
132
133
134
135 func Copy[T any](s, t []T) int {
136 i := 0
137 for ; i < len(s) && i < len(t); i++ {
138 s[i] = t[i]
139 }
140 return i
141 }
142
View as plain text