-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sinks.go
291 lines (271 loc) · 7.44 KB
/
sinks.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
package iterutil
import (
"cmp"
"iter"
"golang.org/x/exp/constraints"
)
// IsEmpty reports whether seq is an empty iterator.
func IsEmpty[E any](seq iter.Seq[E]) bool {
for range seq {
return false
}
return true
}
// Len returns the number of elements in seq.
// It terminates if and only if seq is finite.
func Len[E any](seq iter.Seq[E]) int {
var n int
for range seq {
n++
}
return n
}
// At, if count is non-negative, returns
// the element at index n in seq and true
// or the zero value and false if seq contains fewer than n-1 elements;
// otherwise, it panics.
func At[I constraints.Integer, E any](seq iter.Seq[E], n I) (e E, ok bool) {
if n < 0 {
panic("cannot be negative")
}
for v := range seq {
if 0 < n {
n--
continue
}
e = v
ok = true
return
}
return
}
// Equal reports whether two iterators are equal:
// the same length and all elements equal.
// If the lengths are different, Equal returns false.
// Otherwise, the elements are compared sequentially,
// and the comparison stops at the first unequal pair.
// Floating point NaNs are not considered equal.
// Equal may not terminate if seq1 or seq2 or both are infinite.
func Equal[E comparable](seq1, seq2 iter.Seq[E]) bool {
return EqualFunc(seq1, seq2, equal)
}
func equal[E comparable](e1, e2 E) bool { return e1 == e2 }
// EqualFunc reports whether two iterators are equal using eq as equality
// function on each pair of elements.
// If the lengths are different, EqualFunc returns false.
// Otherwise, the elements are compared sequentially,
// and the comparison stops at the first pair for which eq returns false.
// EqualFunc may not terminate if seq1 or seq2 or both are infinite.
func EqualFunc[A, B comparable](seq1 iter.Seq[A], seq2 iter.Seq[B], eq func(A, B) bool) bool {
next1, stop1 := iter.Pull(seq1)
defer stop1()
next2, stop2 := iter.Pull(seq2)
defer stop2()
for {
v1, ok1 := next1()
v2, ok2 := next2()
if !ok1 {
return !ok2
}
if ok1 != ok2 || !eq(v1, v2) {
return false
}
}
}
// Contains report whether target is present in seq.
// It may not terminate if seq is infinite.
func Contains[E comparable](seq iter.Seq[E], target E) bool {
for e := range seq {
if e == target {
return true
}
}
return false
}
// ContainsFunc reports whether at least one element e of seq satisfies p(e).
// It may not terminate if seq is infinite.
func ContainsFunc[E any](seq iter.Seq[E], p func(E) bool) bool {
for e := range seq {
if p(e) {
return true
}
}
return false
}
// Min, if seq is not empty, returns the minimal value in seq and true;
// otherwise, it returns the zero value and false.
// For floating-point numbers, Min propagates NaNs
// (any NaN value in seq forces the output to be NaN).
// Min terminates if and only if seq is finite.
func Min[E cmp.Ordered](seq iter.Seq[E]) (E, bool) {
var (
m E
firstSeen bool
)
for e := range seq {
if !firstSeen {
m = e
firstSeen = true
continue
}
m = min(e, m)
}
return m, firstSeen
}
// MinFunc, if seq is not empty, returns the minimal value
// (using cmp as comparison function) in seq and true;
// otherwise, it returns the zero value and false.
// If there is more than one minimal element according
// to the cmp function, MinFunc returns the first one.
// MinFunc terminates if and only if seq is finite.
func MinFunc[E any](seq iter.Seq[E], cmp func(E, E) int) (E, bool) {
var (
m E
firstSeen bool
)
for e := range seq {
if !firstSeen {
m = e
firstSeen = true
continue
}
if cmp(e, m) < 0 {
m = e
}
}
return m, firstSeen
}
// Max, if seq is not empty, returns the maximal value in seq and true;
// otherwise, it returns the zero value and false.
// For floating-point numbers, Max propagates NaNs
// (any NaN value in seq forces the output to be NaN).
// Max terminates if and only if seq is finite.
func Max[E cmp.Ordered](seq iter.Seq[E]) (E, bool) {
var (
m E
nonEmpty bool
)
for e := range seq {
nonEmpty = true
m = max(e, m)
}
return m, nonEmpty
}
// MaxFunc, if seq is not empty, returns the maximal value
// (using cmp as comparison function) in seq and true;
// otherwise, it returns the zero value and false.
// If there is more than one maximal element according
// to the cmp function, MaxFunc returns the first one.
// MaxFunc terminates if and only if seq is finite.
func MaxFunc[E any](seq iter.Seq[E], cmp func(E, E) int) (E, bool) {
var (
m E
nonEmpty bool
)
for e := range seq {
nonEmpty = true
if cmp(e, m) > 0 {
m = e
}
}
return m, nonEmpty
}
// Compare compares the elements of seq1 and seq2,
// using [cmp.Compare] on each pair of elements.
// The elements are compared sequentially until one element is not equal to
// the other.
// The result of comparing the first non-matching elements is returned.
// If seq1 and seq2 are equal until one of them ends,
// the shorter one is considered less than the longer one.
// The result is 0 if seq1 == seq2, -1 if seq1 < seq2, and +1 if seq1 > seq2.
// For floating-point types, a NaN is considered less than any non-NaN,
// and -0.0 is not less than (is equal to) 0.0.
// It may not terminate if seq1 or seq2 or both are infinite.
func Compare[E cmp.Ordered](seq1, seq2 iter.Seq[E]) int {
return CompareFunc(seq1, seq2, cmp.Compare)
}
// CompareFunc is like [Compare] but uses a custom comparison function on each
// pair of elements.
// The result is the first non-zero result of cmp;
// if cmp always returns 0, the result is 0 if len(seq1) == len(seq2),
// -1 if len(seq1) < len(seq2), and +1 if len(seq1) > len(seq2).
// It may not terminate if seq1 or seq2 or both are infinite.
func CompareFunc[A, B any](seq1 iter.Seq[A], seq2 iter.Seq[B], cmp func(A, B) int) int {
next1, stop1 := iter.Pull(seq1)
defer stop1()
next2, stop2 := iter.Pull(seq2)
defer stop2()
for {
v1, ok1 := next1()
v2, ok2 := next2()
switch {
case !ok1 && ok2:
return -1
case !ok1 && !ok2:
return 0
case ok1 && !ok2:
return 1
default:
if c := cmp(v1, v2); c != 0 {
return c
}
}
}
}
// IsSorted reports whether seq is sorted in ascending order.
// For floating-point types, a NaN is considered less than any non-NaN,
// and -0.0 is not less than (is equal to) 0.0.
// It may not terminate if seq is infinite.
func IsSorted[E cmp.Ordered](seq iter.Seq[E]) bool {
var (
last E
firstSeen bool
)
for e := range seq {
if firstSeen && cmp.Less(e, last) {
return false
}
last = e
firstSeen = true
}
return true
}
// IsSortedFunc reports whether seq is sorted in ascending order,
// using cmp as comparison function.
// It may not terminate if seq is infinite.
func IsSortedFunc[E any](seq iter.Seq[E], cmp func(E, E) int) bool {
var (
last E
firstSeen bool
)
for e := range seq {
if firstSeen && cmp(e, last) < 0 {
return false
}
last = e
firstSeen = true
}
return true
}
// Reduce performs a [left-associative] [fold] of seq using
// b as the initial value and
// f as the left-associative binary operation.
// It terminates if and only if seq is finite.
//
// [fold]: https://en.wikipedia.org/wiki/Fold_(higher-order_function)
// [left-associative]: https://en.wikipedia.org/wiki/Associative_property#Notation_for_non-associative_operations
func Reduce[A, B any](seq iter.Seq[A], b B, f func(B, A) B) B {
for a := range seq {
b = f(b, a)
}
return b
}
// Len2 returns the number of elements in seq.
// It terminates if and only if seq is finite.
func Len2[K, V any](seq iter.Seq2[K, V]) int {
var n int
for range seq {
n++
}
return n
}