-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
183 lines (148 loc) · 3.77 KB
/
list.go
File metadata and controls
183 lines (148 loc) · 3.77 KB
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
package list
import (
"fmt"
"math/rand"
"reflect"
"sort"
)
// List is a generic list type.
type List[T any] struct {
items []T
}
// SliceToList converts a slice to a list.
func SliceToList[T any](items []T) List[T] {
return List[T]{items: items}
}
// Len returns the length of the list.
func (l *List[T]) Len() int {
return len(l.items)
}
// Get returns the item at the given index.
func (l *List[T]) Get(i int) T {
return l.items[i]
}
// Set sets the item at the given index.
func (l *List[T]) Set(i int, item T) *List[T] {
l.items[i] = item
return l
}
// Append adds items to the end of list.
func (l *List[T]) Append(items ...T) *List[T] {
l.items = append(l.items, items...)
return l
}
// Prepend adds items to the beginning of list.
func (l *List[T]) Prepend(items ...T) *List[T] {
return l.Insert(0, items...)
}
// Remove removes the item at the given index.
func (l *List[T]) Remove(i int) *List[T] {
l.items = append(l.items[:i], l.items[i+1:]...)
return l
}
// Insert adds items at the given index.
func (l *List[T]) Insert(i int, items ...T) *List[T] {
l.items = append(l.items[:i], append(items, l.items[i:]...)...)
return l
}
// Slice returns the list as a slice.
func (l *List[T]) Slice() []T {
return l.items
}
// Map overwrites the list with the result of applying the given function to each item.
func (l *List[T]) Map(f func(T) T) *List[T] {
for i, item := range l.items {
l.items[i] = f(item)
}
return l
}
// Filter removes all items from the list that do not match the given predicate.
func (l *List[T]) Filter(f func(T) bool) *List[T] {
var filtered []T
for _, item := range l.items {
if f(item) {
filtered = append(filtered, item)
}
}
l.items = filtered
return l
}
// Reduce reduces the list to a single value by applying the given function to each item.
func (l *List[T]) Reduce(f func(T, T) T) T {
if len(l.items) == 0 {
return reflect.Zero(reflect.TypeOf(*new(T))).Interface().(T)
}
acc := l.items[0]
for _, item := range l.items[1:] {
acc = f(acc, item)
}
return acc
}
// ForEach iterates over the list and calls the given function for each item.
func (l List[T]) ForEach(f func(T)) {
for _, item := range l.items {
f(item)
}
}
// Contains returns true if the list contains the given item.
func (l List[T]) Contains(item T) bool {
for _, i := range l.items {
if reflect.DeepEqual(i, item) {
return true
}
}
return false
}
// IndexOf returns the index of the first occurrence of the given item.
func (l List[T]) IndexOf(item T) int {
for j, i := range l.items {
if reflect.DeepEqual(i, item) {
return j
}
}
return -1
}
// Reverse reverses the list.
func (l *List[T]) Reverse() *List[T] {
for i := len(l.items)/2 - 1; i >= 0; i-- {
opp := len(l.items) - 1 - i
l.items[i], l.items[opp] = l.items[opp], l.items[i]
}
return l
}
// Clear removes all items from the list.
func (l *List[T]) Clear() *List[T] {
l.items = l.items[:0]
return l
}
// Copy returns a new copy of the list.
// Useful when you want to modify a list without modifying the original.
func (l *List[T]) Copy() *List[T] {
items := make([]T, len(l.items))
copy(items, l.items)
return &List[T]{items: items}
}
// Sort sorts the list using the given function.
func (l *List[T]) Sort(f func(T, T) bool) *List[T] {
sort.Slice(l.items, func(i, j int) bool {
return f(l.items[i], l.items[j])
})
return l
}
// Shuffle shuffles the list.
// You need to seed the random number generator yourself.
func (l *List[T]) Shuffle() *List[T] {
for i := range l.items {
j := rand.Intn(i + 1)
l.items[i], l.items[j] = l.items[j], l.items[i]
}
return l
}
// Swap swaps the items at the given indices.
func (l *List[T]) Swap(i, j int) *List[T] {
l.items[i], l.items[j] = l.items[j], l.items[i]
return l
}
func (l List[T]) String() string {
return fmt.Sprint(l.items)
}