From bbfce6abe98136bd81cc221084a2e680b6b8c993 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 16 Apr 2023 23:40:30 +0800 Subject: [PATCH] chore: refactor max/min in fx (#3135) --- core/fx/stream.go | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/core/fx/stream.go b/core/fx/stream.go index 8a637164..cebb11e3 100644 --- a/core/fx/stream.go +++ b/core/fx/stream.go @@ -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 -}