chore: refactor max/min in fx (#3135)

master
Kevin Wan 2 years ago committed by GitHub
parent 0d11ce03a8
commit bbfce6abe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -292,6 +292,18 @@ func (s Stream) Map(fn MapFunc, opts ...Option) Stream {
}, opts...)
}
// Max returns the maximum item from the underlying source.
func (s Stream) Max(less LessFunc) any {
var max any
for item := range s.source {
if max == nil || less(max, item) {
max = item
}
}
return max
}
// Merge merges all the items into a slice and generates a new stream.
func (s Stream) Merge() Stream {
var items []any
@ -306,6 +318,18 @@ func (s Stream) Merge() Stream {
return Range(source)
}
// Min returns the minimum item from the underlying source.
func (s Stream) Min(less LessFunc) any {
var min any
for item := range s.source {
if min == nil || less(item, min) {
min = item
}
}
return min
}
// NoneMatch returns whether all elements of this stream don't match the provided predicate.
// May not evaluate the predicate on all elements if not necessary for determining the result.
// If the stream is empty then true is returned and the predicate is not evaluated.
@ -540,25 +564,3 @@ func newOptions() *rxOptions {
workers: defaultWorkers,
}
}
// Max returns the maximum item from the underlying source.
func (s Stream) Max(less LessFunc) any {
var maxItem any = nil
for item := range s.source {
if maxItem == nil || less(maxItem, item) {
maxItem = item
}
}
return maxItem
}
// Min returns the minimum item from the underlying source.
func (s Stream) Min(less LessFunc) any {
var minItem any = nil
for item := range s.source {
if minItem == nil || !less(minItem, item) {
minItem = item
}
}
return minItem
}

Loading…
Cancel
Save