-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminals.go
More file actions
152 lines (140 loc) · 3.91 KB
/
Copy pathterminals.go
File metadata and controls
152 lines (140 loc) · 3.91 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
package iters
import (
"cmp"
"iter"
)
// ForEach invokes the consumer function for each item of the iter.Seq.
func ForEach[T any](input iter.Seq[T], consumer func(T)) {
for v := range input {
consumer(v)
}
}
// Reduce performs a reduction on the elements of the input Seq, using an associative
// accumulation function, and returns a value describing the reduced value, if any.
// If no reduced value (e.g. because the iter.Seq is empty), the second returned value
// is false.
func Reduce[T any](input iter.Seq[T], accumulator func(a, b T) T) (T, bool) {
pull, stop := iter.Pull(input)
defer stop()
accum, ok := pull()
if !ok {
return accum, false
}
for r, ok := pull(); ok; r, ok = pull() {
accum = accumulator(accum, r)
}
return accum, true
}
// AllMatch returns whether all elements of this iter.Seq match the provided predicate.
// If this operation finds an item where the predicate is false, it stops processing
// the rest of the iter.Seq.
func AllMatch[T any](input iter.Seq[T], predicate func(T) bool) bool {
for r := range input {
if !predicate(r) {
return false
}
}
return true
}
// AnyMatch returns whether any elements of the iter.Seq match the provided predicate.
// If this operation finds an item where the predicate is true, it stops processing
// the rest of the iter.Seq.
func AnyMatch[T any](input iter.Seq[T], predicate func(T) bool) bool {
for r := range input {
if predicate(r) {
return true
}
}
return false
}
// NoneMatch returns whether no elements of the iter.Seq match the provided predicate.
// If this operation finds an item where the predicate is true, it stops processing
// the rest of the iter.Seq.
func NoneMatch[T any](input iter.Seq[T], predicate func(T) bool) bool {
return !AnyMatch(input, predicate)
}
// Count of elements in the iter.Seq.
func Count[T any](input iter.Seq[T]) int {
c := 0
for range input {
c++
}
return c
}
// FindFirst returns the first element of this iter.Seq along with true or, if the
// iter.Seq is empty, the zero value of the inner type along with false.
func FindFirst[T any](input iter.Seq[T]) (T, bool) {
for i := range input {
return i, true
}
var t T
return t, false
}
// Max returns the maximum element of the iter.Seq.
// along with true if the iter.Seq is not empty. If the iter.Seq is empty, returns the zero
// value along with false.
func Max[T cmp.Ordered](input iter.Seq[T]) (T, bool) {
it, stop := iter.Pull(input)
defer stop()
max, ok := it()
if !ok {
return max, false
}
for n, ok := it(); ok; n, ok = it() {
if n > max {
max = n
}
}
return max, true
}
// MaxFunc returns the maximum element of the iter.Seq according to the provided Comparator,
// along with true if the iter.Seq is not empty. If the iter.Seq is empty, returns the zero
// value along with false.
func MaxFunc[T any](input iter.Seq[T], cmp func(a, b T) int) (T, bool) {
it, stop := iter.Pull(input)
defer stop()
max, ok := it()
if !ok {
return max, false
}
for n, ok := it(); ok; n, ok = it() {
if cmp(n, max) > 0 {
max = n
}
}
return max, true
}
// Min returns the minimum element of the iter.Seq,
// along with true if the iter.Seq is not empty. If the iter.Seq is empty, returns the zero
// value along with false.
func Min[T cmp.Ordered](input iter.Seq[T]) (T, bool) {
next, stop := iter.Pull(input)
defer stop()
min, ok := next()
if !ok {
return min, false
}
for n, ok := next(); ok; n, ok = next() {
if n < min {
min = n
}
}
return min, true
}
// MinFunc returns the minimum element of the iter.Seq according to the provided Comparator,
// along with true if the iter.Seq is not empty. If the iter.Seq is empty, returns the zero
// value along with false.
func MinFunc[T any](input iter.Seq[T], cmp func(a, b T) int) (T, bool) {
next, stop := iter.Pull(input)
defer stop()
min, ok := next()
if !ok {
return min, false
}
for n, ok := next(); ok; n, ok = next() {
if cmp(n, min) < 0 {
min = n
}
}
return min, true
}