-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
241 lines (218 loc) · 5.22 KB
/
slice.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
package sop
import (
"math/rand"
"sort"
"time"
)
// slice wraps a native slice to perform
// operations on it.
type slice[T any] struct {
s []T
}
var _ Enumerable[any] = (*slice[any])(nil)
// Slice packs a given slice []T into a
// *slice[T] object.
func Slice[T any](s []T) *slice[T] {
return &slice[T]{s}
}
// Unwrap returns the originaly packed
// slice []T of the Slice[T] object.
func (s *slice[T]) Unwrap() []T {
return s.s
}
// Len returns the length of the given Slice.
func (s *slice[T]) Len() int {
return len(s.s)
}
// Each performs the given function f on each
// element in the Slice.
//
// f is getting passed the value v at the
// current position as well as the current
// index i.
func (s *slice[T]) Each(f func(v T, i int)) {
notNil("f", f)
for i, v := range s.s {
f(v, i)
}
}
// Filter performs preticate p on each element
// in the Slice and each element where p
// returns true will be added to the result
// slice.
//
// p is getting passed the value v at the
// current position as well as the current
// index i.
func (s *slice[T]) Filter(p func(v T, i int) bool) Enumerable[T] {
notNil("p", p)
res := newSliceFrom[T, T](s)
var j int
s.Each(func(v T, i int) {
if p(v, i) {
res[j] = v
j++
}
})
return Slice(res[:j])
}
// Any returns true when at least one element in
// the given slice result in a true return of p
// when performed on p.
func (s *slice[T]) Any(p func(v T, i int) bool) bool {
notNil("p", p)
for i, v := range s.s {
if p(v, i) {
return true
}
}
return false
}
// All returns true when all elements in the given
// slice result in a true return of p when performed
// on p.
func (s *slice[T]) All(p func(v T, i int) bool) bool {
notNil("p", p)
return !s.Any(func(v T, i int) bool {
return !p(v, i)
})
}
// All returns true when all elements in the given
// slice result in a true return of p when performed
// on p.
func (s *slice[T]) None(p func(v T, i int) bool) bool {
notNil("p", p)
return !s.Any(p)
}
// First returns the value and index of the first
// occurence in the Enumerable where preticate p
// returns true.
//
// If this applies to no element in the Enumerable,
// default of T and -1 is returned.
func (s *slice[T]) First(p func(v T, i int) bool) (rv T, ri int) {
ri = -1
s.Any(func(v T, i int) bool {
ok := p(v, i)
if ok {
rv = v
ri = i
}
return ok
})
return
}
// Count returns the number of elements in the given
// slice which, when applied on p, return true.
func (s *slice[T]) Count(p func(v T, i int) bool) (c int) {
notNil("p", p)
s.Each(func(v T, i int) {
if p(v, i) {
c++
}
})
return
}
// Shuffle re-arranges the given slice in a pseudo-random
// order and returns the result slice.
//
// You can also pass a custom random source rngSrc if
// you desire.
func (s *slice[T]) Shuffle(rngSrc ...rand.Source) (res Enumerable[T]) {
var rng *rand.Rand
if len(rngSrc) != 0 {
rng = rand.New(rngSrc[0])
} else {
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
}
src := copySlice(s.s)
r := Slice(newSliceFrom[T, T](s))
for i := len(src) - 1; i > 0; i-- {
j := rng.Intn(i + 1)
r.s[i] = src[j]
src = append(src[:j], src[j+1:]...)
}
r.s[0] = src[0]
res = r
return
}
// Sort re-orders the slice x given the provided less function.
func (s *slice[T]) Sort(less func(p, q T, i int) bool) Enumerable[T] {
notNil("less", less)
res := copySlice(s.s)
sort.Slice(res, func(i, j int) bool {
return less(res[i], res[j], i)
})
return Slice(res)
}
// Aggregate applies tze multiplicator function f over
// all elements of the given Slice and returns the final
// result.
func (s *slice[T]) Aggregate(f func(a, b T) T) (c T) {
notNil("f", f)
if s.Len() == 0 {
return
}
c = s.s[0]
for i := 1; i < s.Len(); i++ {
c = f(c, s.s[i])
}
return
}
// Push appends the passed value v to the Slice.
func (s *slice[T]) Push(v T) {
s.s = append(s.s, v)
}
// Pop removes the last element of the Slice and
// returns its value. If the Slice is empty, the
// default value of T is returned.
func (s *slice[T]) Pop() (res T) {
if s.Len() == 0 {
return
}
res = s.s[len(s.s)-1]
s.s = s.s[:len(s.s)-1]
return
}
// Append adds all elements of Slice v to the
// end of the current slice.
func (s *slice[T]) Append(v Enumerable[T]) {
s.s = append(s.s, v.Unwrap()...)
}
// Flush removes all elements of the given Slice.
func (s *slice[T]) Flush() {
s.s = make([]T, 0)
}
// Slice removes the values from the given slice
// starting at i with the amount of n. The removed
// slice is returned as new Slice.
func (s *slice[T]) Splice(i, n int) (res Enumerable[T]) {
t := copySlice(s.s)
res = Slice(t[i : i+n])
s.s = append(s.s[:i], s.s[i+n:]...)
return
}
// At safely accesses the element in the Enumerable
// at the given index i and returns it, if existent.
// If there is no value at i, default of T and false
// is returned.
func (s *slice[T]) At(i int) (v T, ok bool) {
if i < 0 || i >= s.Len() {
return
}
v = s.s[i]
ok = true
return
}
// Replace safely replaces the value in the Enumerable
// at the given index i with the given value v and
// returns true if the value was replaced. If the
// Enumerable has no value at i, false is returned.
func (s *slice[T]) Replace(i int, v T) (ok bool) {
if i < 0 || i >= s.Len() {
return
}
s.s[i] = v
ok = true
return
}