-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
436 lines (366 loc) · 11.7 KB
/
collection.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package collection
import (
"bytes"
"encoding/json"
"math"
"math/rand"
"reflect"
"sort"
"sync"
"time"
)
type Collection[T any] struct {
items []T
}
// New returns a new collection of type T containing the specified
// items and their types. ( Chainable )
func New[T any](items ...T) *Collection[T] {
return &Collection[T]{
items: items,
}
}
// Items returns the current collection's set of items.
func (c *Collection[T]) Items() []T {
return c.items
}
// Sort sorts the collection given the provided less function. ( Chainable )
func (c *Collection[T]) Sort(less func(i, j int) bool) *Collection[T] {
sort.Slice(c.items, less)
return c
}
// Filter returns a new collection with items that have passed predicate check.
// ( Chainable )
func (c *Collection[T]) Filter(f func(T) bool) (out Collection[T]) {
for _, inner := range c.items {
if f(inner) {
out.Push(inner)
}
}
return out
}
// Batch exposes a very simple parallelized batch processor for a collection.
// Defining a `batchSize` will break the current collection into groups of
// jobs that will be processed in parallel by Goroutines managed by an error
// group. The specified function `f` will be executed for each job in each
// batch. The signature for this function is
// `func(currentBatchIndex, currentJobIndex int, job T)`. ( Chainable )
func (c *Collection[T]) Batch(f func(int, int, T), batchSize int) *Collection[T] {
var (
batches [][]T
batchCount = int(math.Ceil(float64(c.Length()) / float64(batchSize)))
wg sync.WaitGroup
)
jobsCount := c.Length()
if batchSize > jobsCount {
batchSize = jobsCount
}
offset, limit := 0, batchSize
for i := 0; i < batchCount; i++ {
batches = append(batches, c.items[offset:limit])
offset, limit = limit, limit+batchSize
if limit > c.Length() {
limit = c.Length()
}
}
for b, batch := range batches {
wg.Add(len(batch))
for j, t := range batch {
go func(b int, j int, t T) {
defer wg.Done()
f(b, j, t)
}(b, j, t)
}
wg.Wait()
}
return c
}
// Slice returns a new collection containing a slice of the current collection
// starting with `from` and `to` indexes. ( Chainable )
func (c *Collection[T]) Slice(from, to int) *Collection[T] {
if from > to {
from = to
}
if to > c.Length() {
to = c.Length()
}
return New(c.items[from:to]...)
}
// Contains returns true if an item is present in the current collection. This
// method makes use of `reflect.DeepEqual` to ensure an absolute match. If you
// wish to check by a specific field within a slice of objects, use
// `collection.ContainsBy` instead.
func (c *Collection[T]) Contains(item T) (found bool) {
for _, inner := range c.items {
if reflect.DeepEqual(item, inner) {
found = true
break
}
}
return found
}
// ContainsBy returns true if an item in the current collection matches the
// specified predicate function. This is useful if you have a slice of objects
// and you wish to check the existence of a specific field value.
func (c *Collection[T]) ContainsBy(f func(i int, item T) bool) (found bool) {
for i, item := range c.items {
if f(i, item) {
found = true
break
}
}
return found
}
// PushDistinct method appends one or more distinct items to the current
// collection, returning the new length. Items that already exist within the
// current collection will be ignored. You can check for this by comparing old
// v.s. new collection lengths.
func (c *Collection[T]) PushDistinct(items ...T) int {
for _, item := range items {
if !c.Contains(item) {
c.Push(item)
}
}
return c.Length()
}
// Shift method removes the first item from the current collection, then
// returns that item.
func (c *Collection[T]) Shift() T {
out := c.items[:1][0]
c.items = c.items[1:]
return out
}
// Unshift method appends one item to the beginning of the current collection,
// returning the new length of the collection.
func (c *Collection[T]) Unshift(item T) int {
c.items = append([]T{item}, c.items...)
return c.Length()
}
// At attempts to return the item associated with the specified index for the
// current collection along with a boolean value stating whether or not an item
// could be found.
func (c *Collection[T]) At(index int) (T, bool) {
if index > (c.Length()-1) || index < 0 {
var out T
return out, false
}
return c.items[index], true
}
// IsEmpty returns a boolean value describing the empty state of the current
// collection.
func (c *Collection[T]) IsEmpty() bool {
return c.Length() <= 0
}
// Empty will reset the current collection to zero items. ( Chainable )
func (c *Collection[T]) Empty() *Collection[T] {
c.items = nil
return c
}
// Find returns the first item in the provided current collectionthat satisfies
// the provided testing function. If no items satisfy the testing function,
// a <nil> value is returned.
func (c *Collection[T]) Find(f func(i int, item T) bool) (item T) {
for i, item := range c.items {
if found := f(i, item); found {
return item
}
}
return item
}
// FindIndex returns the index of the first item in the specified collection
// that satisfies the provided testing function. Otherwise, it returns -1,
// indicating that no element passed the test.
func (c *Collection[T]) FindIndex(f func(i int, item T) bool) int {
for i, item := range c.items {
if found := f(i, item); found {
return i
}
}
return -1
}
// RandomIndex returns the index associated with a random item from the current
// collection.
func (c *Collection[T]) RandomIndex() int {
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Intn(c.Length() - 1)
}
// Random returns a random item from the current collection.
func (c *Collection[T]) Random() (T, bool) {
rand.New(rand.NewSource(time.Now().UnixNano()))
return c.At(rand.Intn(c.Length()))
}
// LastIndexOf returns the last index at which a given item can be found in the
// current collection, or -1 if it is not present.
func (c *Collection[T]) LastIndexOf(item T) int {
index := -1
for i, inner := range c.items {
if reflect.DeepEqual(item, inner) {
index = i
}
}
return index
}
// Reduce reduces a collection to a single value. The value is calculated by
// accumulating the result of running each item in the collection through an
// accumulator function. Each successive invocation is supplied with the return
// value returned by the previous call.
func (c *Collection[T]) Reduce(f func(i int, item, accumulator T) T) (out T) {
for i, item := range c.items {
out = f(i, item, out)
}
return out
}
// Reverse the current collection so that the first item becomes the last, the
// second item becomes the second to last, and so on. ( Chainable )
func (c *Collection[T]) Reverse() *Collection[T] {
for i1, i2 := 0, c.Length()-1; i1 < i2; i1, i2 = i1+1, i2-1 {
c.items[i1], c.items[i2] = c.items[i2], c.items[i1]
}
return c
}
// Some returns a true value if at least one item within the current collection
// resolves to true as defined by the predicate function f.
func (c *Collection[T]) Some(f func(i int, item T) bool) bool {
for i, item := range c.items {
if found := f(i, item); found {
return true
}
}
return false
}
// None returns a true value if no items within the current collection resolve to
// true as defined by the predicate function f.
func (c *Collection[T]) None(f func(i int, item T) bool) bool {
count := 0
for i, item := range c.items {
if found := f(i, item); !found {
count++
}
}
return count == c.Length()
}
// All returns a true value if all items within the current collection resolve to
// true as defined by the predicate function f.
func (c *Collection[T]) All(f func(i int, item T) bool) bool {
count := 0
for i, item := range c.items {
if found := f(i, item); found {
count++
}
}
return count == c.Length()
}
// Push method appends one or more items to the end of a collection, returning
// the new length.
func (c *Collection[T]) Push(items ...T) int {
c.items = append(c.items, items...)
return c.Length()
}
// Pop method removes the last item from the current collection and then
// returns that item.
func (c *Collection[T]) Pop() (out T, found bool) {
if c.Length() == 0 {
return
}
out = c.items[c.Length()-1]
c.items = c.items[0 : c.Length()-1]
return out, true
}
// Length returns number of items associated with the current collection.
func (c *Collection[T]) Length() int {
return len(c.items)
}
// Map method creates to a new collection by using callback invocation result
// on each array item. On each iteration f is invoked with arguments: index and
// current item. It should return the new collection. ( Chainable )
func (c *Collection[T]) Map(f func(int, T) T) (out Collection[T]) {
for i, item := range c.items {
out.Push(f(i, item))
}
return out
}
// Each iterates through the specified list of items executes the specified
// callback on each item. This method returns the current instance of
// collection. ( Chainable )
func (c *Collection[T]) Each(f func(int, T) bool) *Collection[T] {
for i, item := range c.items {
if exit := f(i, item); exit {
break
}
}
return c
}
// Concat merges two slices of items. This method returns the current instance
// collection with the specified slice of items appended to it. ( Chainable )
func (c *Collection[T]) Concat(items []T) *Collection[T] {
c.items = append(c.items, items...)
return c
}
// InsertAt inserts the specified item at the specified index and returns the
// current collection. If the specified index is less than 0, 0 is used. If an
// index greater than the size of the collectio nis specified, c.Push is used
// instead. ( Chainable )
func (c *Collection[T]) InsertAt(item T, index int) *Collection[T] {
if index <= 0 {
c.Unshift(item)
return c
}
if index > (c.Length() - 1) {
c.Push(item)
return c
}
c.items = append(c.items[:index+1], c.items[index:]...)
c.items[index] = item
return c
}
// InsertBefore inserts the specified item before the specified index and
// returns the current collection. If the specified index is less than 0,
// c.Unshift is used. If an index greater than the size of the collection is
// specified, c.Push is used instead. ( Chainable )
func (c *Collection[T]) InsertBefore(item T, index int) *Collection[T] {
return c.InsertAt(item, index-1)
}
// InsertAfter inserts the specified item after the specified index and returns
// the current collection. If the specified index is less than 0, 0 is used. If
// an index greater than the size of the collectio nis specified, c.Push is used
// instead. ( Chainable )
func (c *Collection[T]) InsertAfter(item T, index int) *Collection[T] {
return c.InsertAt(item, index+1)
}
// AtFirst attempts to return the first item of the collection along with a
// boolean value stating whether or not an item could be found.
func (c *Collection[T]) AtFirst() (T, bool) {
return c.At(0)
}
// AtLast attempts to return the last item of the collection along with a
// boolean value stating whether or not an item could be found.
func (c *Collection[T]) AtLast() (T, bool) {
return c.At(c.Length() - 1)
}
// Count counts the number of items in the collection that compare equal to value.
func (c *Collection[T]) Count(item T) (count int) {
for _, inner := range c.items {
if reflect.DeepEqual(inner, item) {
count++
}
}
return
}
// CountBy counts the number of items in the collection for which predicate is true.
func (c *Collection[T]) CountBy(f func(T) bool) (count int) {
for _, item := range c.items {
if f(item) {
count++
}
}
return count
}
// MarshalJSON implements the Marshaler interface so the current collection's
// items can be marshalled into valid JSON.
func (c *Collection[T]) MarshalJSON() ([]byte, error) {
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
if err := encoder.Encode(c.Items()); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}