-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patharrayOperations.go
231 lines (202 loc) · 4.81 KB
/
arrayOperations.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package go2
// FindOne iterates through an array applying the guard function to each element and returns the first element that passes.
// If no such element is found, it returns the zero value and false.
//
// arr := []int{1,2,3,4}
// func isEven(i int) bool {
// return i % 2 == 0
// }
// fmt.Println(FindOne[int](arr, isEven))
// // output: [2, 4]
func FindOne[T any](arr []T, guard func(T) bool) (T, bool) {
for idx := range arr {
if guard(arr[idx]) {
return arr[idx], true
}
}
return *new(T), false
}
// Reduce iterates through an array applying the function to each element and the cumulative value and the final state of the cumulative value
//
// arr := []string{"cat","dog","cat","cow"}
// func countAnimals(state map[string]int, animal string) map[string]int {
// count, ok := state[animal]
// if !ok {
// state[animal] = 1
// return state
// }
//
// state[animal] = count + 1
// return state
// }
// initialState := make(map[string]int)
// fmt.Println(Reduce[string, map[string]int](arr, countAnimals, initialState))
// // output: map["cat":2 "dog":1 "cow":1]
func Reduce[T, A any](arr []T, fn func(A, T) A, init A) A {
ret := init
for idx := range arr {
ret = fn(ret, arr[idx])
}
return ret
}
// Filter iterates through an array applying the guard function to each element and returns the elements that pass
//
// arr := []int{0,1,2,3,4}
// func isEven(i int) bool {
// return i % 2 == 0
// }
// fmt.Println(Filter[int](arr, isEven))
// // output: [0,2,4]
func Filter[T any](arr []T, guard func(T) bool) []T {
var ret []T
for idx := range arr {
if guard(arr[idx]) {
ret = append(ret, arr[idx])
}
}
return ret
}
// Map iterates through an array applying the transform function to each element and returns the modified array
//
// arr := []int{1,2,3}
// func addTen(i int) int {
// return i + 10
// }
// fmt.Println(Map[int](arr, addTen))
// // output: [11, 12, 13]
func Map[T any](arr []T, transform func(T) T) []T {
ret := make([]T, len(arr))
for idx := range arr {
ret[idx] = transform(arr[idx])
}
return ret
}
// Distinct returns the unique vals of a slice
//
// fmt.Println(Distinct[int]([]int{1, 1, 2, 3}))
// // output: [1, 2, 3]
func Distinct[T comparable](arrs ...[]T) []T {
// put the values of our slice into a map
// the key's of the map will be the slice's unique values
m := make(map[T]struct{})
for idx1 := range arrs {
for idx2 := range arrs[idx1] {
m[arrs[idx1][idx2]] = struct{}{}
}
}
// create the output slice and populate it with the map's keys
res := make([]T, len(m))
i := 0
for k := range m {
res[i] = k
i++
}
return res
}
// Intersect returns a slice of values that are present in all of the input slices
//
// // example #1
// a := []int{1, 1, 3, 4, 5, 6}
// b := []int{2, 3, 6}
// fmt.Println(Intersect[int](a, b))
// // output: []int{3, 6}
//
// // example #2
// fmt.Println(Intersect[int]([]int{1, 1, 3, 4, 5, 6}))
// // output: []int{1, 3, 4, 5, 6}
func Intersect[T comparable](arrs ...[]T) []T {
m := make(map[T]int)
var (
tmpArr []T
count int
ok bool
)
for idx1 := range arrs {
tmpArr = Distinct(arrs[idx1])
for idx2 := range tmpArr {
count, ok = m[tmpArr[idx2]]
if !ok {
m[tmpArr[idx2]] = 1
} else {
m[tmpArr[idx2]] = count + 1
}
}
}
var (
ret []T
lenArrs int = len(arrs)
)
for k, v := range m {
if v == lenArrs {
ret = append(ret, k)
}
}
return ret
}
// Union returns a slice that contains the unique values of all the input slices
//
// // example #1
// a := []int{1, 2, 2, 4, 6}
// b := []int{2, 4, 5}
// fmt.Println(Union[int](a, b))
// // output: []int{1, 2, 4, 5, 6}
//
// // example #2
// fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6}))
// // output: []int{1, 3, 4, 5, 6}
func Union[T comparable](arrs ...[]T) []T {
m := make(map[T]struct{})
var tmpArr []T
for idx := range arrs {
tmpArr = Distinct(arrs[idx])
for idx2 := range tmpArr {
m[tmpArr[idx2]] = struct{}{}
}
}
ret := make([]T, len(m))
i := 0
for k := range m {
ret[i] = k
i++
}
return ret
}
// Difference returns a slice of values that are only present in one of the input slices
//
// // example #1
// a := []int{1, 2, 2, 4, 6}
// b := []int{2, 4, 5}
// fmt.Println(Difference[int](a, b))
// // output: []int{1, 5, 6}
//
// // example #2
// fmt.Println(Difference[int]([]int{1, 1, 3, 4, 5, 6}))
// // output: []int{1, 3, 4, 5, 6}
func Difference[T comparable](arrs ...[]T) []T {
m := make(map[T]int)
var (
tmpArr []T
count int
ok bool
)
for idx1 := range arrs {
tmpArr = Distinct(arrs[idx1])
for idx2 := range tmpArr {
count, ok = m[tmpArr[idx2]]
if !ok {
m[tmpArr[idx2]] = 1
} else {
m[tmpArr[idx2]] = count + 1
}
}
}
var (
ret []T
)
for k, v := range m {
if v == 1 {
ret = append(ret, k)
}
}
return ret
}