-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsequence_functions.go
226 lines (207 loc) · 7.08 KB
/
sequence_functions.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
/*
* Copyright (c) 2022, [email protected]
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package sequences
import (
"github.com/nwillc/genfuncs"
"github.com/nwillc/genfuncs/container"
"github.com/nwillc/genfuncs/container/maps"
"github.com/nwillc/genfuncs/results"
"strings"
)
// All returns true if all elements in the sequence match the predicate.
func All[T any](sequence container.Sequence[T], predicate genfuncs.Function[T, bool]) (result bool) {
result = true
iterator := sequence.Iterator()
for iterator.HasNext() {
if !predicate(iterator.Next()) {
result = false
break
}
}
return result
}
// Any returns true if any element of the sequence matches the predicate.
func Any[T any](sequence container.Sequence[T], predicate genfuncs.Function[T, bool]) (result bool) {
result = false
iterator := sequence.Iterator()
for iterator.HasNext() {
if predicate(iterator.Next()) {
result = true
break
}
}
return result
}
// Associate returns a map containing key/values created by applying a function to each value of the container.Iterator
// returned by the container.Sequence.
func Associate[T any, K comparable, V any](sequence container.Sequence[T], keyValueFor maps.KeyValueFor[T, K, V]) (result *genfuncs.Result[container.GMap[K, V]]) {
iterator := sequence.Iterator()
m := make(container.GMap[K, V])
for iterator.HasNext() {
keyValueFor(iterator.Next()).
OnSuccess(func(kv *maps.Entry[K, V]) {
m[kv.Key] = kv.Value
})
}
return genfuncs.NewResult(m)
}
// AssociateWith returns a Map where keys are elements from the given sequence and values are produced by the
// valueSelector function applied to each element.
func AssociateWith[K comparable, V any](sequence container.Sequence[K], valueFor maps.ValueFor[K, V]) (result *genfuncs.Result[container.GMap[K, V]]) {
iterator := sequence.Iterator()
m := make(container.GMap[K, V])
var t K
for iterator.HasNext() {
t = iterator.Next()
value := valueFor(t)
if !value.Ok() {
return results.MapError[V, container.GMap[K, V]](value)
}
m[t] = value.OrEmpty()
}
return genfuncs.NewResult(m)
}
// Collect elements from a Sequence into a Container.
func Collect[T any](s container.Sequence[T], c container.Container[T]) {
iterator := s.Iterator()
for iterator.HasNext() {
c.Add(iterator.Next())
}
}
// Compare two sequences with a comparator returning less/equal/greater (-1/0/1) and return comparison of the two.
func Compare[T any](s1, s2 container.Sequence[T], comparator func(t1, t2 T) int) int {
i1 := s1.Iterator()
i2 := s2.Iterator()
for i1.HasNext() && i2.HasNext() {
cmp := comparator(i1.Next(), i2.Next())
if cmp != genfuncs.EqualTo {
return cmp
}
}
if i2.HasNext() {
return genfuncs.LessThan
}
if i1.HasNext() {
return genfuncs.GreaterThan
}
return genfuncs.EqualTo
}
// Distinct collects a sequence into a container.Set and returns it as a Sequence.
func Distinct[T comparable](s container.Sequence[T]) container.Sequence[T] {
set := container.NewMapSet[T]()
Collect[T](s, set)
return set
}
// Find returns the first element matching the given predicate, or Result error of NoSuchElement if not found.
func Find[T any](sequence container.Sequence[T], predicate genfuncs.Function[T, bool]) *genfuncs.Result[T] {
iterator := sequence.Iterator()
var result T
for iterator.HasNext() {
result = iterator.Next()
if predicate(result) {
return genfuncs.NewResult(result)
}
}
return genfuncs.NewError[T](genfuncs.NoSuchElement)
}
// FindLast returns the last element matching the given predicate, or Result error of NoSuchElement if not found.
func FindLast[T any](sequence container.Sequence[T], predicate genfuncs.Function[T, bool]) *genfuncs.Result[T] {
iterator := sequence.Iterator()
result := genfuncs.NewError[T](genfuncs.NoSuchElement)
var t T
for iterator.HasNext() {
t = iterator.Next()
if predicate(t) {
result = genfuncs.NewResult(t)
}
}
return result
}
// FlatMap returns a sequence of all elements from results of valueFor being invoked on each element of
// original sequence, and those resultant slices concatenated.
func FlatMap[T, R any](sequence container.Sequence[T], transform genfuncs.Function[T, container.Sequence[R]]) (result container.Sequence[R]) {
return container.NewIteratorSequence(newFlatMapIterator(sequence, transform))
}
// Fold accumulates a value starting with an initial value and applying operation to each value of the container.Iterator
// returned by the container.Sequence.
func Fold[T, R any](sequence container.Sequence[T], initial R, operation genfuncs.BiFunction[R, T, R]) (result R) {
iterator := sequence.Iterator()
result = initial
for iterator.HasNext() {
result = operation(result, iterator.Next())
}
return result
}
// ForEach calls action for each element of a Sequence.
func ForEach[T any](sequence container.Sequence[T], action func(t T)) {
iterator := sequence.Iterator()
for iterator.HasNext() {
action(iterator.Next())
}
}
// IsSorted returns true if the GSlice is sorted by order.
func IsSorted[T any](sequence container.Sequence[T], order genfuncs.BiFunction[T, T, bool]) (ok bool) {
iterator := sequence.Iterator()
var current, last T
ok = true
first := true
for iterator.HasNext() {
current = iterator.Next()
if first {
first = false
} else {
if order(current, last) {
ok = false
break
}
}
last = current
}
return ok
}
// JoinToString creates a string from all the elements of a Sequence using the stringer on each, separating them using separator, and
// using the given prefix and postfix.
func JoinToString[T any](
sequence container.Sequence[T],
stringer genfuncs.ToString[T],
separator string,
prefix string,
postfix string,
) string {
var sb strings.Builder
iterator := sequence.Iterator()
sb.WriteString(prefix)
first := true
for iterator.HasNext() {
if first {
first = false
} else {
sb.WriteString(separator)
}
sb.WriteString(stringer(iterator.Next()))
}
sb.WriteString(postfix)
return sb.String()
}
// Map elements in a Sequence to a new Sequence having applied the valueFor to them.
func Map[T, R any](sequence container.Sequence[T], transform genfuncs.Function[T, R]) container.Sequence[R] {
return container.NewIteratorSequence[R](transformIterator[T, R]{iterator: sequence.Iterator(), transform: transform})
}
// NewSequence creates a sequence from the provided values.
func NewSequence[T any](values ...T) (sequence container.Sequence[T]) {
var slice container.GSlice[T] = values
return slice
}