-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstream.txt
255 lines (203 loc) · 12.6 KB
/
stream.txt
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
package stream // import "github.com/mariomac/gostream/stream"
Package stream provides type-safe streams, functional helper tools and
processing operations
FUNCTIONS
func AllMatch[T any](input Stream[T], predicate func(T) bool) bool
AllMatch returns whether all elements of this stream match the provided
predicate. If this operation finds an item where the predicate is false, it
stops processing the rest of the stream. This function is equivalent to
invoking input.AllMatch(predicate) as method
func AnyMatch[T any](input Stream[T], predicate func(T) bool) bool
AnyMatch returns whether any elements of this stream match the provided
predicate. If this operation finds an item where the predicate is true, it
stops processing the rest of the stream. This function is equivalent to
invoking input.AnyMatch(predicate) as method
func Count[T any](input Stream[T]) int
Count of elements in this stream. This function is equivalent to invoking
input.Count() as method.
func FindFirst[T any](input Stream[T]) (T, bool)
FindFirst returns the first element of this Stream along with true or, if
the stream is empty, the zero value of the inner type along with false. This
function is equivalent to invoking input.FindFirst() as method.
func ForEach[T any](input Stream[T], consumer func(T))
ForEach invokes the consumer function for each item of the Stream. This
function is equivalent to invoking input.ForEach(consumer) as method.
func Max[T any](input Stream[T], cmp order.Comparator[T]) (T, bool)
Max returns the maximum element of this stream according to the provided
Comparator, along with true if the stream is not empty. If the stream is
empty, returns the zero value along with false. This function is equivalent
to invoking input.Max(cmp) as method.
func Min[T any](input Stream[T], cmp order.Comparator[T]) (T, bool)
Min returns the minimum element of this stream according to the provided
Comparator, along with true if the stream is not empty. If the stream is
empty, returns the zero value along with false. This function is equivalent
to invoking input.Min(cmp) as method.
func NoneMatch[T any](input Stream[T], predicate func(T) bool) bool
NoneMatch returns whether no elements of this stream match the provided
predicate. If this operation finds an item where the predicate is true, it
stops processing the rest of the stream. This function is equivalent to
invoking input.NoneMatch(predicate) as method.
func Reduce[T any](input Stream[T], accumulator func(a, b T) T) (T, bool)
Reduce performs a reduction on the elements of this stream, using an
associative accumulation function, and returns an value describing the
reduced value, if any. If no reduced value (e.g. because the stream is
empty), the second returned value is false. This function is equivalent to
invoking input.Reduce(accumulator) as method.
func ToMap[K comparable, V any](input Stream[item.Pair[K, V]]) map[K]V
ToMap returns a map Containing all the item.Pair elements of this Stream,
where the Key/Val fields of the item.Pair represents the key/value of the
map, respectively.
func ToSlice[T any](input Stream[T]) []T
ToSlice returns a Slice Containing all the elements of this Stream. This
function is equivalent to invoking input.ToSlice() as method.
TYPES
type Stream[T any] interface {
// Filter returns a Stream consisting of the items of this stream that match the given
// predicate (this is, applying the predicate function over the item returns true).
Filter(predicate func(T) bool) Stream[T]
// FlatMap returns a stream consisting of the results of replacing each element of this stream
// with the contents of a mapped stream produced by applying the provided mapping function to
// each element. Each mapped stream is closed after its contents have been placed into this
// stream. (If a mapped stream is null an empty stream is used, instead.)
//
// Due to the lazy nature of streams, if any of the mapped streams is infinite it will remain
// unnoticed and some operations (Count, Reduce, Sorted, AllMatch...) will not end.
//
// If you need that the input and output Stream contain elements from different types, you need
// to invoke the standalone function FlatMap[IN,OUT](Stream[IN], func(IN)OUT) Stream[OUT].
FlatMap(mapper func(T) Stream[T]) Stream[T]
// Limit returns a stream consisting of the elements of this stream, truncated to
// be no longer than maxSize in length.
Limit(maxSize int) Stream[T]
// Map returns a Stream consisting of the results of individually applying
// the mapper function to each element of this Stream. The argument and return
// value of the mapper function must belong to the same type. If you need that
// the input and output Stream contain elements from different types, you need
// to invoke the standalone function Map[IN,OUT](Stream[IN], func(IN)OUT) Stream[OUT].
Map(mapper func(T) T) Stream[T]
// Peek peturns a stream consisting of the elements of this stream, additionally performing
// the provided action on each element as elements are consumed from the resulting stream.
Peek(consumer func(T)) Stream[T]
// Skip returns a stream consisting of the remaining elements of this stream after discarding
// the first n elements of the stream.
Skip(n int) Stream[T]
// Sorted returns a stream consisting of the elements of this stream, sorted according
// to the provided order.Comparator.
Sorted(comparator order.Comparator[T]) Stream[T]
// AllMatch returns whether all elements of this stream match the provided predicate.
// If this operation finds an item where the predicate is false, it stops processing
// the rest of the stream.
AllMatch(predicate func(T) bool) bool
// AnyMatch returns whether any elements of this stream match the provided predicate.
// If this operation finds an item where the predicate is true, it stops processing
// the rest of the stream.
AnyMatch(predicate func(T) bool) bool
// Count of elements in this stream.
Count() int
// FindFirst returns the first element of this Stream along with true or, if the
// stream is empty, the zero value of the inner type along with false.
FindFirst() (T, bool)
// ForEach invokes the consumer function for each item of the Stream.
ForEach(consumer func(T))
// Max returns the maximum element of this stream according to the provided Comparator,
// along with true if the stream is not empty. If the stream is empty, returns the zero
// value along with false.
Max(cmp order.Comparator[T]) (T, bool)
// Min returns the minimum element of this stream according to the provided Comparator,
// along with true if the stream is not empty. If the stream is empty, returns the zero
// value along with false.
Min(cmp order.Comparator[T]) (T, bool)
// NoneMatch returns whether no elements of this stream match the provided predicate.
// If this operation finds an item where the predicate is true, it stops processing
// the rest of the stream.
NoneMatch(predicate func(T) bool) bool
// Reduce performs a reduction on the elements of this stream, using an associative
// accumulation function, and returns an value describing the reduced value, if any.
// If no reduced value (e.g. because the stream is empty), the second returned value
// is false.
Reduce(accumulator func(a, b T) T) (T, bool)
// ToSlice returns a Slice Containing all the elements of this Stream.
ToSlice() []T
// Has unexported methods.
}
Stream is a sequence of elements supporting different processing and
aggregation functionalities. To perform a computation, Stream operations are
composed into a stream pipeline. A stream pipeline consists of a source
(which might be an array, a collection, a generator function, an I/O
channel, etc), zero or more intermediate operations (which transform a
stream into another stream, such as filter(predicate)), and a terminal
operation (which produces a result or side-effect, such as reduce(function)
or forEach(consumer)). Streams are lazy; computation on the source data is
only performed when the terminal operation is initiated, and source elements
are consumed only as needed.
func Concat[T any](a, b Stream[T]) Stream[T]
Concat creates a lazily concatenated stream whose elements are all the
elements of the first stream followed by all the elements of the second
stream.
func Distinct[T comparable](input Stream[T]) Stream[T]
Distinct returns a stream consisting of the distinct elements (according to
equality operator) of the input stream.
func Empty[T any]() Stream[T]
Empty returns an empty stream
func Filter[T any](input Stream[T], predicate func(T) bool) Stream[T]
Filter returns a Stream consisting of the items of this stream that match
the given predicate (this is, applying the predicate function over the item
returns true). This function is equivalent to invoking
input.Filter(predicate) as method.
func FlatMap[IN, OUT any](input Stream[IN], mapper func(IN) Stream[OUT]) Stream[OUT]
FlatMap returns a stream consisting of the results of replacing each element
of this stream with the contents of a mapped stream produced by applying the
provided mapping function to each element. Each mapped stream is closed
after its contents have been placed into this stream. (If a mapped stream is
null an empty stream is used, instead.)
Due to the lazy nature of streams, if any of the mapped streams is infinite
it will remain unnoticed and some operations (Count, Reduce, Sorted,
AllMatch...) will not end.
When both the input and output type are the same, the operation can be
invoked as the method input.FlatMap(mapper).
func Generate[T any](supplier func() T) Stream[T]
Generate an infinite sequential stream where each element is generated by
the provided supplier function. Due to the stateful nature of the supplier,
multiple operations towards the same stream might provide different results.
func Iterate[T any](seed T, f func(T) T) Stream[T]
Iterate returns an infinite sequential ordered Stream produced by iterative
application of a function f to an initial element seed, producing a Stream
consisting of seed, f(seed), f(f(seed)), etc. The first element (position 0)
in the Stream will be the provided seed. For n > 0, the element at position
n, will be the result of applying the function f to the element at position
n - 1. Due to the stateful nature of the supplier, multiple operations
towards the same stream might provide different results.
func Limit[T any](input Stream[T], maxSize int) Stream[T]
Limit returns a stream consisting of the elements of this stream, truncated
to be no longer than maxSize in length. This function is equivalent to
invoking input.Limit(maxSize) as method.
func Map[IT, OT any](input Stream[IT], mapper func(IT) OT) Stream[OT]
Map returns a Stream consisting of the results of individually applying the
mapper function to each elements of the input Stream. When both the input
and output type are the same, the operation can be invoked as the method
input.Map(mapper).
func Of[T any](elems ...T) Stream[T]
Of creates an Stream from a variable number of elements that are passed as
arguments.
func OfChannel[T any](source <-chan T) Stream[T]
OfChannel creates a Stream from an input channel. The Stream won't end until
the source channel is closed, so some operations (Distinct, Sorted, ToSlice,
Count...) will block the execution until the source is closed.
func OfMap[K comparable, V any](source map[K]V) Stream[item.Pair[K, V]]
OfMap creates a Stream of item.Pair elements. Each kv.Pair corresponds to a
key/value entry of the source map.
func OfSlice[T any](elems []T) Stream[T]
OfSlice creates a Stream from a slice.
func Peek[T any](input Stream[T], consumer func(T)) Stream[T]
Peek peturns a stream consisting of the elements of this stream,
additionally performing the provided action on each element as elements are
consumed from the resulting stream. This function is equivalent to invoking
input.Peek(consumer) as method.
func Skip[T any](input Stream[T], n int) Stream[T]
Skip returns a stream consisting of the remaining elements of this stream
after discarding the first n elements of the stream. This function is
equivalent to invoking input.Skip(n) as method.
func Sorted[T any](input Stream[T], comparator order.Comparator[T]) Stream[T]
Sorted returns a stream consisting of the elements of this stream, sorted
according to the provided order.Comparator. This function is equivalent to
invoking input.Sorted(comparator) as method.