-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs.go
255 lines (221 loc) · 4.65 KB
/
funcs.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package enu
import (
"sort"
"golang.org/x/exp/constraints"
)
func Each[T any](e IEnumerable[T], iteratee func(item T, index int)) {
each(e, func(item T, index int) bool {
iteratee(item, index)
return true
})
}
func each[T any](enumerable IEnumerable[T], iteratee func(item T, index int) bool) {
iter := enumerable.GetEnumerator()
defer iter.Dispose()
index := 0
for {
item, ok := iter.Next()
if !ok {
break
}
if !iteratee(item, index) {
break
}
index++
}
}
func Nth[T any](e IEnumerable[T], nth int) (T, bool) {
if nth < 0 {
collection := ToSlice(e)
l := len(collection)
if l < -nth {
return empty[T](), false
}
return collection[l+nth], true
}
result := empty[T]()
ok := false
each(e, func(item T, index int) bool {
if nth == index {
ok = true
result = item
return false
}
return true
})
return result, ok
}
func Count[T any](e IEnumerable[T]) int {
v := 0
each(e, func(item T, _ int) bool {
v += 1
return true
})
return v
}
func Find[T any](e IEnumerable[T], predicate func(T, int) bool) (T, bool) {
result := empty[T]()
ok := false
each(e, func(item T, index int) bool {
if predicate(item, index) {
result = item
ok = true
return false
}
return true
})
return result, ok
}
func First[T any](e IEnumerable[T]) (T, bool) {
result := empty[T]()
ok := false
each(e, func(item T, _ int) bool {
result = item
ok = true
return false
})
return result, ok
}
func Last[T any](e IEnumerable[T]) (T, bool) {
result := ToSlice(e)
if len(result) == 0 {
return empty[T](), false
}
return result[len(result)-1], true
}
func ToSlice[T any](e IEnumerable[T]) []T {
result := []T{}
Each(e, func(item T, _ int) {
result = append(result, item)
})
return result
}
func Reverse[T any](e IEnumerable[T]) *SliceEnumerator[T] {
collection := ToSlice(e)
length := len(collection)
half := length / 2
for i := 0; i < half; i = i + 1 {
j := length - 1 - i
collection[i], collection[j] = collection[j], collection[i]
}
return NewSliceEnumerator(collection)
}
func Sort[T constraints.Ordered](e IEnumerable[T]) *SliceEnumerator[T] {
res := ToSlice(e)
sort.SliceStable(res, func(i, j int) bool {
return res[i] < res[j]
})
return NewSliceEnumerator(res)
}
func SortBy[T any](e IEnumerable[T], sorter func(i, j T) bool) *SliceEnumerator[T] {
res := ToSlice(e)
sort.SliceStable(res, func(i, j int) bool {
return sorter(res[i], res[j])
})
return NewSliceEnumerator(res)
}
func Sum[T constraints.Integer | constraints.Float | constraints.Complex](e IEnumerable[T]) T {
var sum T = 0
for _, val := range ToSlice(e) {
sum += val
}
return sum
}
func Min[T constraints.Ordered](e IEnumerable[T]) T {
var min T
collection := ToSlice(e)
if len(collection) == 0 {
return empty[T]()
}
min = collection[0]
for _, item := range collection {
if min > item {
min = item
}
}
return min
}
func Max[T constraints.Ordered](e IEnumerable[T]) T {
var max T
collection := ToSlice(e)
if len(collection) == 0 {
return empty[T]()
}
max = collection[0]
for _, item := range collection {
if max < item {
max = item
}
}
return max
}
func Uniq[T comparable](e IEnumerable[T]) *UniqEnumerator[T] {
return &UniqEnumerator[T]{iter: e.GetEnumerator()}
}
func Contains[T comparable](e IEnumerable[T], element T) bool {
ok := false
each(e, func(item T, _ int) bool {
if item == element {
ok = true
return false
}
return true
})
return ok
}
func IndexOf[T comparable](e IEnumerable[T], element T) int {
i := -1
each(e, func(item T, index int) bool {
if item == element {
i = index
return false
}
return true
})
return i
}
func IsAll[T any](e IEnumerable[T], predicate func(item T) bool) bool {
flag := true
each(e, func(item T, _ int) bool {
if !predicate(item) {
flag = false
return false
}
return true
})
return flag
}
func IsAny[T any](e IEnumerable[T], predicate func(item T) bool) bool {
flag := false
each(e, func(item T, _ int) bool {
if predicate(item) {
flag = true
return false
}
return true
})
return flag
}
func Filter[T any](e IEnumerable[T], predicate func(item T, index int) bool) *FilterEnumerator[T] {
return &FilterEnumerator[T]{
iter: e.GetEnumerator(),
predicate: predicate,
}
}
func Reject[T any](e IEnumerable[T], predicate func(item T, index int) bool) *RejectEnumerator[T] {
return &RejectEnumerator[T]{
iter: e.GetEnumerator(),
predicate: predicate,
}
}
func Take[T any](e IEnumerable[T], size int) *TakeEnumerator[T] {
return &TakeEnumerator[T]{
iter: e.GetEnumerator(),
size: size,
}
}
func Result[T any](e IEnumerable[T], out *[]T) {
Each(e, func(item T, _ int) {
*out = append(*out, item)
})
}