-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
167 lines (144 loc) · 3.55 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
package x
type Collection[V comparable] struct {
items []V
}
func Collect[V comparable](items []V) *Collection[V] {
return &Collection[V]{items: items}
}
func (c *Collection[V]) Merge(items []V) *Collection[V] {
var cNew = &Collection[V]{items: make([]V, len(c.items))}
for idx := range c.items {
cNew.items[idx] = c.items[idx]
}
cNew.items = append(cNew.items, items...)
return cNew
}
func (c *Collection[V]) Replace(old V, new V, n int) *Collection[V] {
var cNew = &Collection[V]{items: make([]V, len(c.items))}
copy(cNew.items, c.items)
for idx := range cNew.items {
if n == 0 {
break
}
if cNew.items[idx] == old {
cNew.items[idx] = new
n--
}
}
return cNew
}
func (c *Collection[V]) Diff(items []V) *Collection[V] {
itemMp := map[V]struct{}{}
for _, item := range items {
itemMp[item] = struct{}{}
}
cNew := &Collection[V]{items: []V{}}
for _, item := range c.items {
_, ok := itemMp[item]
if !ok {
cNew.items = append(cNew.items, item)
}
}
return cNew
}
func (c *Collection[V]) DiffBy(items []V, iteratee func(V) V) *Collection[V] {
itemMp := map[V]struct{}{}
for _, item := range items {
itemMp[iteratee(item)] = struct{}{}
}
cNew := &Collection[V]{items: []V{}}
for _, item := range c.items {
_, ok := itemMp[iteratee(item)]
if !ok {
cNew.items = append(cNew.items, item)
}
}
return cNew
}
func (c *Collection[V]) DiffWith(items []V, iteratee func(V) V) *Collection[V] {
itemMp := map[V]struct{}{}
for _, item := range items {
itemMp[iteratee(item)] = struct{}{}
}
cNew := &Collection[V]{items: []V{}}
for _, item := range c.items {
_, ok := itemMp[iteratee(item)]
if !ok {
cNew.items = append(cNew.items, item)
}
}
return cNew
}
func (c *Collection[V]) ForEach(iteratee func(value V, idx int)) *Collection[V] {
itemsL := len(c.items)
for idx := 0; idx < itemsL; idx++ {
iteratee(c.items[idx], idx)
}
return c
}
func (c *Collection[V]) ForEachRight(iteratee func(value V, idx int)) *Collection[V] {
for i := len(c.items) - 1; 0 <= i; i-- {
iteratee(c.items[i], i)
}
return c
}
func (c *Collection[V]) Map(iteratee func(value V, idx int) V) *Collection[V] {
var cNew = &Collection[V]{items: make([]V, len(c.items))}
for idx := range c.items {
cNew.items[idx] = iteratee(c.items[idx], idx)
}
return cNew
}
func (c *Collection[V]) Filter(predicate func(value V, idx int) bool) *Collection[V] {
var cNew = &Collection[V]{items: []V{}}
for k, v := range c.items {
if !predicate(v, k) {
continue
}
cNew.items = append(cNew.items, v)
}
return cNew
}
func (c *Collection[V]) Reverse() *Collection[V] {
var cNew = &Collection[V]{items: []V{}}
for i := len(c.items) - 1; 0 <= i; i-- {
cNew.items = append(cNew.items, c.items[i])
}
return cNew
}
// Output
func (c *Collection[V]) ToSlice() []V {
items := make([]V, len(c.items))
copy(items, c.items)
return items
}
func (c *Collection[V]) Chunk(size int) [][]V {
itemsL := len(c.items)
chunksL := (itemsL + size - 1) / size
var chunks = make([][]V, chunksL)
for idx := 0; idx < chunksL; idx++ {
hi := (idx + 1) * size
if itemsL < hi {
hi = itemsL
}
chunks[idx] = append([]V{}, c.items[idx*size:hi]...)
}
return chunks
}
// Statistic
func (c *Collection[V]) Sum(iteratee func(value V) int) int {
itemsL := len(c.items)
var total int
for idx := 0; idx < itemsL; idx++ {
total += iteratee(c.items[idx])
}
return total
}
func (c *Collection[V]) Avg(iteratee func(value V) int) int {
itemsL := len(c.items)
var total int
for idx := 0; idx < itemsL; idx++ {
total += iteratee(c.items[idx])
}
return total / itemsL
}