-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.go
322 lines (287 loc) · 9.67 KB
/
list.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
package steams
import (
"slices"
"sort"
"github.com/javiorfo/nilo"
)
// List is a generic type that represents a slice of elements of type T.
// It provides methods to perform various operations on the list, following a functional programming style.
type List[T any] []T
// ListOf creates a List from a variadic list of elements of type T and returns it as a Steam.
func ListOf[T any](args ...T) Steam[T] {
return List[T](args)
}
// Filter returns a new List containing only the elements that match the provided predicate function.
func (list List[T]) Filter(predicate func(T) bool) Steam[T] {
results := make(List[T], 0)
for _, v := range list {
if predicate(v) {
results = append(results, v)
}
}
return results
}
// MapToAny applies the provided mapper function to each element in the List and returns a new List of type any.
// If result to specific type is needed, use integration function Mapping[T, R](s Steam[T], mapper func(T) R)
func (list List[T]) MapToAny(mapper func(T) any) Steam[any] {
results := make(List[any], len(list))
for i, v := range list {
results[i] = mapper(v)
}
return results
}
// MapToString applies the provided mapper function to each element in the List and returns a new List of strings.
func (list List[T]) MapToString(mapper func(T) string) Steam[string] {
results := make(List[string], len(list))
for i, v := range list {
results[i] = mapper(v)
}
return results
}
// MapToInt applies the provided mapper function to each element in the List and returns a new List of integers.
func (list List[T]) MapToInt(mapper func(T) int) Steam[int] {
results := make(List[int], len(list))
for i, v := range list {
results[i] = mapper(v)
}
return results
}
// FilterMapToAny filters the elements based on the provided predicate and then maps the remaining elements
// using the provided mapper function, returning a new List of type any.
func (list List[T]) FilterMapToAny(predicate func(T) bool, mapper func(T) any) Steam[any] {
results := make(List[any], 0)
for _, v := range list {
if predicate(v) {
results = append(results, mapper(v))
}
}
return results
}
// FilterMapToInt filters the elements based on the provided predicate and then maps the remaining elements
// using the provided mapper function, returning a new List of type int.
func (list List[T]) FilterMapToInt(predicate func(T) bool, mapper func(T) int) Steam[int] {
results := make(List[int], 0)
for _, v := range list {
if predicate(v) {
results = append(results, mapper(v))
}
}
return results
}
// FilterMapToString filters the elements based on the provided predicate and then maps the remaining elements
// using the provided mapper function, returning a new List of type string.
func (list List[T]) FilterMapToString(predicate func(T) bool, mapper func(T) string) Steam[string] {
results := make(List[string], 0)
for _, v := range list {
if predicate(v) {
results = append(results, mapper(v))
}
}
return results
}
// FlatMapToAny applies the provided mapper function to each element in the List, which returns a Steam,
// and concatenates the results into a single List of type any.
func (list List[T]) FlatMapToAny(mapper func(T) Steam[any]) Steam[any] {
results := make(List[any], 0, list.Count())
for _, v := range list {
results = slices.Concat(results, mapper(v).(List[any]))
}
return results
}
// FlatMapToInt applies the provided mapper function to each element in the List, which returns a Steam,
// and concatenates the results into a single List of type int.
func (list List[T]) FlatMapToInt(mapper func(T) Steam[int]) Steam[int] {
results := make(List[int], 0, list.Count())
for _, v := range list {
results = slices.Concat(results, mapper(v).(List[int]))
}
return results
}
// FlatMapToString applies the provided mapper function to each element in the List, which returns a Steam,
// and concatenates the results into a single List of type string.
func (list List[T]) FlatMapToString(mapper func(T) Steam[string]) Steam[string] {
results := make(List[string], 0, list.Count())
for _, v := range list {
results = slices.Concat(results, mapper(v).(List[string]))
}
return results
}
// Limit restricts the number of elements in the List to the specified limit and returns a new List.
func (list List[T]) Limit(limit int) Steam[T] {
if limit > list.Count() {
limit = list.Count()
}
results := make(List[T], limit)
copy(results, list[:limit])
return results
}
// Count returns the number of elements in the List.
func (list List[T]) Count() int {
return len(list)
}
// ForEach applies the provided consumer function to each element in the List.
func (list List[T]) ForEach(consumer func(T)) {
for _, v := range list {
consumer(v)
}
}
// ForEachWithIndex applies the provided index and consumer function to each element in the List.
func (list List[T]) ForEachWithIndex(consumer func(int, T)) {
for i, v := range list {
consumer(i, v)
}
}
// Peek applies the provided consumer function to each element in the List without modifying it,
// and returns the original List.
func (list List[T]) Peek(consumer func(T)) Steam[T] {
for _, v := range list {
consumer(v)
}
return list
}
// AllMatch checks if all elements in the List match the provided predicate function.
// It returns true if all elements match, false otherwise.
func (list List[T]) AllMatch(predicate func(T) bool) bool {
for _, v := range list {
if !predicate(v) {
return false
}
}
return true
}
// AnyMatch checks if any element in the List matches the provided predicate function.
// It returns true if at least one element matches, false otherwise.
func (list List[T]) AnyMatch(predicate func(T) bool) bool {
return slices.ContainsFunc(list, predicate)
}
// NoneMatch checks if no elements in the List match the provided predicate function.
// It returns true if no elements match, false otherwise.
func (list List[T]) NoneMatch(predicate func(T) bool) bool {
return !slices.ContainsFunc(list, predicate)
}
// FindFirst returns an Optional containing the first element of the List if it is present;
// otherwise, it returns an empty Optional.
func (list List[T]) FindFirst() nilo.Optional[T] {
if len(list) > 0 {
return nilo.Of(list[0])
}
return nilo.Empty[T]()
}
// FindOne returns a nilo.Optional[T] that match the given predicate function.
func (list List[T]) FindOne(predicate func(T) bool) nilo.Optional[T] {
for _, v := range list {
if predicate(v) {
return nilo.Of(v)
}
}
return nilo.Empty[T]()
}
// TakeWhile returns a new List containing elements from the start of the List
// as long as they match the provided predicate function.
// It stops including elements as soon as an element does not match.
func (list List[T]) TakeWhile(predicate func(T) bool) Steam[T] {
results := make(List[T], 0)
for _, v := range list {
if predicate(v) {
results = append(results, v)
} else {
break
}
}
return results
}
// DropWhile returns a new List that skips elements from the start of the List
// as long as they match the provided predicate function.
// It includes all subsequent elements after the first non-matching element.
func (list List[T]) DropWhile(predicate func(T) bool) Steam[T] {
results := make(List[T], 0)
for _, v := range list {
if !predicate(v) {
results = append(results, v)
}
}
return results
}
// Reduce applies an accumulator function to the elements of the List,
// starting with the provided initial value. It returns the final accumulated value.
func (list List[T]) Reduce(initValue T, acc func(T, T) T) T {
result := initValue
for _, v := range list {
result = acc(result, v)
}
return result
}
// Reverse returns a new List containing the elements of the original List in reverse order.
func (list List[T]) Reverse() Steam[T] {
length := len(list)
results := make(List[T], length)
index := length - 1
for i := range list {
results[i] = list[index]
index--
}
return results
}
// Position returns an Optional containing the index of the first element that matches the provided predicate function;
// otherwise, it returns an empty Optional.
func (list List[T]) Position(predicate func(T) bool) nilo.Optional[int] {
for i, v := range list {
if predicate(v) {
return nilo.Of(i)
}
}
return nilo.Empty[int]()
}
// Last returns an Optional containing the last element of the List if it is present;
// otherwise, it returns an empty Optional.
func (list List[T]) Last() nilo.Optional[T] {
length := list.Count()
if length > 0 {
return nilo.Of(list[length-1])
}
return nilo.Empty[T]()
}
// Skip returns a new List that skips the first n elements of the original List.
// If n is greater than or equal to the length of the List, it returns an empty List.
func (list List[T]) Skip(n int) Steam[T] {
length := len(list)
if length > n {
length = length - n
} else {
return List[T]{}
}
results := make(List[T], length)
for i := range length {
results[i] = list[i+n]
}
return results
}
// Sorted returns a new List containing the elements of the original List sorted
// according to the provided comparison function.
func (list List[T]) Sorted(cmp func(T, T) bool) Steam[T] {
slice := list.Collect()
results := make(List[T], len(slice))
copy(results, slice)
sort.Slice(results, func(i, j int) bool {
return cmp(results[i], results[j])
})
return results
}
// GetCompared returns an Optional containing the element that is compared
// according to the provided comparison function. If the List is empty, it returns an empty Optional.
func (list List[T]) GetCompared(cmp func(T, T) bool) nilo.Optional[T] {
if len(list) == 0 {
return nilo.Empty[T]()
}
item := list[0]
for i := 1; i < len(list); i++ {
if cmp(list[i], item) {
item = list[i]
}
}
return nilo.Of(item)
}
// Collect returns the underlying slice of the List.
func (list List[T]) Collect() []T {
return list
}