-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcollections.go
242 lines (217 loc) · 5.94 KB
/
collections.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
package gofp
// Map returns a new slice with transformed elements
func Map(items []interface{}, fn func(index int, item interface{}) interface{}) []interface{} {
mappedItems := []interface{}{}
for index, value := range items {
mappedItems = append(mappedItems, fn(index, value))
}
return mappedItems
}
//Fill substitutes the elements of slice with given string from the start to end position
func Fill(args ...interface{}) []interface{} {
var (
filler string
items []interface{}
start int
end int
)
if len(args) < 2 {
panic("Invalid number of arguments has been passed, at least 2 arguments are required")
}
if len(args) >= 1 {
items = args[0].([]interface{})
start = 0
end = len(items)
}
if len(args) >= 2 {
filler = args[1].(string)
}
if len(args) >= 3 {
start = args[2].(int)
}
if len(args) >= 4 {
end = args[3].(int)
}
newItems := []interface{}{}
for index, value := range items {
if index >= start && index < end {
newItems = append(newItems, filler)
continue
}
newItems = append(newItems, value)
}
return newItems
}
// Filter returns a new slice of items which satisfies the condition
func Filter(items []interface{}, fn func(index int, item interface{}) bool) []interface{} {
filteredItems := []interface{}{}
for index, value := range items {
if fn(index, value) {
filteredItems = append(filteredItems, value)
}
}
return filteredItems
}
// Reduce iterate overs all the items in the slice and returns accumulated result
func Reduce(items []interface{}, fn func(index int, current interface{}, accumulator interface{}, source []interface{}) interface{}, initialValue interface{}) interface{} {
accumulator := initialValue
for index, value := range items {
accumulator = fn(index, value, accumulator, items)
}
return accumulator
}
// Every returns true if all the items satisfies the given condition with the function
func Every(items []interface{}, fn func(index int, item interface{}) bool) bool {
for index, value := range items {
if !fn(index, value) {
return false
}
}
return true
}
// Any returns true if any of the item satisfies the given condition with the function
func Any(items []interface{}, fn func(index int, item interface{}) bool) bool {
for index, value := range items {
if fn(index, value) {
return true
}
}
return false
}
// Find returns a item from the slice if that element satisfies the given condition with the function
func Find(items []interface{}, fn func(index int, item interface{}) bool) interface{} {
for index, value := range items {
if fn(index, value) {
return value
}
}
return nil
}
// GroupBy returns a item from the slice if that element satisfies the given condition with the function
func GroupBy(items []interface{}, fn func(item interface{}) string) map[string]interface{} {
group := map[string]interface{}{}
for _, value := range items {
key := fn(value)
if Has(group, key) {
group[key] = append(group[key].([]interface{}), value)
} else {
items := []interface{}{}
group[key] = append(items, value)
}
}
return group
}
// Head returns the first item of slice if exist otherwise nil
func Head(items []interface{}) interface{} {
if len(items) >= 1 {
return items[0]
}
return nil
}
// Tail returns the last item of slice if exist otherwise nil
func Tail(items []interface{}) interface{} {
if len(items) >= 1 {
return items[len(items)-1]
}
return nil
}
// Reverse returns a new slice of reversed items
func Reverse(items []interface{}) []interface{} {
reversed := []interface{}{}
for i := len(items) - 1; i >= 0; i-- {
reversed = append(reversed, items[i])
}
return reversed
}
// Chunk Returns a new slice(chunks) of slices. Every slice has fixed number of elements which was given as a limit in the 2nd parameter
func Chunk(items []interface{}, size int) []interface{} {
chunks := []interface{}{}
startAt := 0
lengthOfItems := len(items)
if lengthOfItems == 0 {
return chunks
}
if size >= lengthOfItems {
return append(chunks, items)
}
for startAt <= lengthOfItems {
upperLimit := startAt + size
if upperLimit < lengthOfItems {
chunks = append(chunks, items[startAt:upperLimit])
} else {
chunks = append(chunks, items[startAt:])
}
startAt = upperLimit
}
return chunks
}
// Range returns a new array with elements starting from min to max
func Range(args ...int) []int {
var (
min = 1
max int
step = 1
)
if len(args) == 1 {
max = args[0]
}
if len(args) >= 2 {
min = args[0]
max = args[1]
if len(args) >= 3 {
step = args[2]
}
}
items := []int{}
for i := min; i <= max; i += step {
items = append(items, i)
}
return items
}
// Uniq returns a new slice of unique items
func Uniq(items []interface{}) []interface{} {
uniqueItems := []interface{}{}
for _, item := range items {
if !Contains(uniqueItems, item) {
uniqueItems = append(uniqueItems, item)
}
}
return uniqueItems
}
// IndexOf returns the poisition of the item in a slice, if item doesn't exist returns -1 otherwise
func IndexOf(items []interface{}, item interface{}) int {
for index, value := range items {
if value == item {
return index
}
}
return -1
}
// Contains returns true if item exists in the slice and false otherwise
func Contains(items []interface{}, item interface{}) bool {
return IndexOf(items, item) > -1
}
//Shuffle returns a new slice with shuffled elements
func Shuffle(items []interface{}) []interface{} {
copiedSlice := make([]interface{}, len(items))
for i := range items {
copiedSlice[i] = items[i]
}
for i := range items {
index := Randomer().Intn(len(items) - 1)
temp := copiedSlice[index]
copiedSlice[index] = copiedSlice[i]
copiedSlice[i] = temp
}
return copiedSlice
}
var prevChosenItem interface{}
//ChooseRandom returns a random element from the slice
func ChooseRandom(items []interface{}) interface{} {
index := Randomer().Intn(len(items) - 1)
for items[index] == prevChosenItem {
index = Randomer().Intn(len(items) - 1)
}
prevChosenItem = items[index]
return items[index]
}