-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
256 lines (232 loc) · 7.94 KB
/
api.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
// Package loop provides helpers for loop operation over key/value pairs.
package loop
import (
"errors"
"github.com/m4gshm/gollections/c"
"github.com/m4gshm/gollections/map_/resolv"
)
// New is the mai breakable key/value loop constructor
func New[S, K, V any](source S, hasNext func(S) bool, getNext func(S) (K, V, error)) Loop[K, V] {
return func() (k K, v V, ok bool, err error) {
if ok := hasNext(source); !ok {
return k, v, false, nil
} else if k, v, err = getNext(source); err != nil {
return k, v, false, err
} else {
return k, v, true, nil
}
}
}
// From wrap the next loop to a breakable loop
func From[K, V any](next func() (K, V, bool)) func() (K, V, bool, error) {
if next == nil {
return nil
}
return func() (K, V, bool, error) {
k, v, ok := next()
return k, v, ok, nil
}
}
// To transforms a breakable loop to a simple loop.
// The errConsumer is a function that is called when an error occurs.
func To[K, V any](next func() (K, V, bool, error), errConsumer func(error)) func() (K, V, bool) {
if next == nil {
return nil
}
return func() (K, V, bool) {
k, v, ok, err := next()
if err != nil {
errConsumer(err)
return k, v, false
}
return k, v, ok
}
}
// Group collects sets of values grouped by keys obtained by passing a key/value loop.
func Group[K comparable, V any](next func() (K, V, bool, error)) (map[K][]V, error) {
return MapResolv(next, resolv.Slice[K, V])
}
// Reduce reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function.
// If the 'next' function returns ok=false at the first call, the zero values of 'K', 'V' types are returned.
func Reduce[K, V any](next func() (K, V, bool, error), merge func(K, K, V, V) (K, V)) (K, V, error) {
rk, rv, _, err := ReduceOK(next, merge)
return rk, rv, err
}
// ReduceOK reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function.
// Returns ok==false if the 'next' function returns ok=false at the first call (no more elements).
func ReduceOK[K, V any](next func() (K, V, bool, error), merge func(K, K, V, V) (K, V)) (rk K, rv V, ok bool, err error) {
if next == nil {
return rk, rv, false, nil
}
k, v, ok, err := next()
if err != nil || !ok {
return k, v, ok, err
}
rk, rv = k, v
for {
k, v, ok, err := next()
if err != nil || !ok {
return rk, rv, true, err
}
rk, rv = merge(rk, k, rv, v)
}
}
// Reducee reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function.
// If the 'next' function returns ok=false at the first call, the zero values of 'K', 'V' types are returned.
func Reducee[K, V any](next func() (K, V, bool, error), merge func(K, K, V, V) (K, V, error)) (K, V, error) {
rk, rv, _, err := ReduceeOK(next, merge)
return rk, rv, err
}
// ReduceeOK reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function.
// Returns ok==false if the 'next' function returns ok=false at the first call (no more elements).
func ReduceeOK[K, V any](next func() (K, V, bool, error), merge func(K, K, V, V) (K, V, error)) (rk K, rv V, ok bool, err error) {
if next == nil {
return rk, rv, false, nil
}
k, v, ok, err := next()
if err != nil || !ok {
return rk, rv, ok, err
}
rk, rv = k, v
for {
if k, v, ok, err := next(); err != nil || !ok {
return rk, rv, true, err
} else if rk, rv, err = merge(rk, k, rv, v); err != nil {
return rk, rv, true, err
}
}
}
// HasAny finds the first key/value pair that satisfies the 'predicate' function condition and returns true if successful
func HasAny[K, V any](next func() (K, V, bool, error), predicate func(K, V) bool) (bool, error) {
_, _, ok, err := First(next, predicate)
return ok, err
}
// HasAnyy finds the first key/value pair that satisfies the 'predicate' function condition and returns true if successful
func HasAnyy[K, V any](next func() (K, V, bool, error), predicate func(K, V) (bool, error)) (bool, error) {
_, _, ok, err := Firstt(next, predicate)
return ok, err
}
// First returns the first key/value pair that satisfies the condition of the 'predicate' function
func First[K, V any](next func() (K, V, bool, error), predicate func(K, V) bool) (K, V, bool, error) {
for {
if k, v, ok, err := next(); err != nil || !ok {
return k, v, false, err
} else if ok := predicate(k, v); ok {
return k, v, true, nil
}
}
}
// Firstt returns the first key/value pair that satisfies the condition of the 'predicate' function
func Firstt[K, V any](next func() (K, V, bool, error), predicate func(K, V) (bool, error)) (K, V, bool, error) {
for {
if k, v, ok, err := next(); err != nil || !ok {
return k, v, false, err
} else if ok, err := predicate(k, v); err != nil || ok {
return k, v, ok, err
}
}
}
// Convert creates a loop that applies the 'converter' function to iterable key\values.
func Convert[K, V any, KOUT, VOUT any](next func() (K, V, bool, error), converter func(K, V) (KOUT, VOUT)) Loop[KOUT, VOUT] {
if next == nil {
return nil
}
return func() (k2 KOUT, v2 VOUT, ok bool, err error) {
k, v, ok, err := next()
if err != nil || !ok {
return k2, v2, false, err
}
k2, v2 = converter(k, v)
return k2, v2, true, nil
}
}
// Conv creates a loop that applies the 'converter' function to iterable key\values.
func Conv[K, V any, KOUT, VOUT any](next func() (K, V, bool, error), converter func(K, V) (KOUT, VOUT, error)) Loop[KOUT, VOUT] {
if next == nil {
return nil
}
return func() (k2 KOUT, v2 VOUT, ok bool, err error) {
k, v, ok, err := next()
if err != nil || !ok {
return k2, v2, false, err
}
k2, v2, err = converter(k, v)
return k2, v2, true, err
}
}
// Filter creates a loop that checks elements by the 'filter' function and returns successful ones.
func Filter[K, V any](next func() (K, V, bool, error), filter func(K, V) bool) Loop[K, V] {
if next == nil {
return nil
}
return func() (K, V, bool, error) {
return First(next, filter)
}
}
// Filt creates a loop that checks elements by the 'filter' function and returns successful ones.
func Filt[K, V any](next func() (K, V, bool, error), filter func(K, V) (bool, error)) Loop[K, V] {
if next == nil {
return nil
}
return func() (K, V, bool, error) {
return Firstt(next, filter)
}
}
// MapResolv collects key\value elements into a new map by iterating over the elements with resolving of duplicated key values
func MapResolv[K comparable, V, VR any](next func() (K, V, bool, error), resolver func(bool, K, VR, V) VR) (map[K]VR, error) {
m := map[K]VR{}
for {
k, v, ok, err := next()
if err != nil || !ok {
return m, err
}
exists, ok := m[k]
m[k] = resolver(ok, k, exists, v)
}
}
// Map collects key\value elements into a new map by iterating over the elements
func Map[K comparable, V any](next func() (K, V, bool, error)) (map[K]V, error) {
return MapResolv(next, resolv.First[K, V])
}
// Slice collects key\value elements to a slice by iterating over the elements
func Slice[K, V, T any](next func() (K, V, bool, error), converter func(K, V) T) ([]T, error) {
if next == nil {
return nil, nil
}
s := []T{}
for {
key, val, ok, err := next()
if ok {
s = append(s, converter(key, val))
}
if !ok || err != nil {
return s, err
}
}
}
// Track applies the 'consumer' function to position/element pairs retrieved by the 'next' function until the consumer returns the c.Break to stop.
func Track[K, V any](next func() (K, V, bool, error), consumer func(K, V) error) error {
if next == nil {
return nil
}
for {
if p, v, ok, err := next(); err != nil || !ok {
return err
} else if err := consumer(p, v); err != nil {
return brk(err)
}
}
}
// Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag.
func Crank[K, V any](next func() (K, V, bool, error)) (n Loop[K, V], k K, v V, ok bool, err error) {
if next != nil {
k, v, ok, err = next()
}
return next, k, v, ok, err
}
func brk(err error) error {
if errors.Is(err, c.Break) {
return nil
}
return err
}