Repository: zeromicro/go-zero
Pull request #3129 (2023-04-14 17:40:42)

feature:  two evaluate operate func addtion in package stream 
1. func Max in package stream returns the maximum item from the underlying source.
2. func Min in package stream returns the minimum item from the underlying source.

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3129?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3129](https://codecov.io/gh/zeromicro/go-zero/pull/3129?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (6e6e2f7) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/73664b92f0f42b47dc8a3035dad572a96369c82c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (73664b9) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3129?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/fx/stream.go](https://codecov.io/gh/zeromicro/go-zero/pull/3129?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9meC9zdHJlYW0uZ28=) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/core/fx/stream.go b/core/fx/stream.go
index 44495f87..8a637164 100644
--- a/core/fx/stream.go
+++ b/core/fx/stream.go
@@ -540,3 +540,25 @@ 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
+}


Test patch:
diff --git a/core/fx/stream_test.go b/core/fx/stream_test.go
index dd58d3e6..c6eaef56 100644
--- a/core/fx/stream_test.go
+++ b/core/fx/stream_test.go
@@ -503,6 +503,83 @@ func TestStream_Concat(t *testing.T) {
 	})
 }
 
+func TestStream_Max(t *testing.T) {
+	runCheckedTest(t, func(t *testing.T) {
+		tests := []struct {
+			name     string
+			elements []any
+			max      any
+		}{
+			{
+				name: "no elements with nil",
+			},
+			{
+				name:     "no elements",
+				elements: []any{},
+				max:      nil,
+			},
+			{
+				name:     "1 element",
+				elements: []any{1},
+				max:      1,
+			},
+			{
+				name:     "multiple elements",
+				elements: []any{1, 2, 9, 5, 8},
+				max:      9,
+			},
+		}
+
+		for _, test := range tests {
+			t.Run(test.name, func(t *testing.T) {
+				val := Just(test.elements...).Max(func(a, b any) bool {
+					return a.(int) < b.(int)
+				})
+				assetEqual(t, test.max, val)
+			})
+		}
+	})
+}
+
+func TestStream_Min(t *testing.T) {
+	runCheckedTest(t, func(t *testing.T) {
+		tests := []struct {
+			name     string
+			elements []any
+			min      any
+		}{
+			{
+				name: "no elements with nil",
+				min:  nil,
+			},
+			{
+				name:     "no elements",
+				elements: []any{},
+				min:      nil,
+			},
+			{
+				name:     "1 element",
+				elements: []any{1},
+				min:      1,
+			},
+			{
+				name:     "multiple elements",
+				elements: []any{-1, 1, 2, 9, 5, 8},
+				min:      -1,
+			},
+		}
+
+		for _, test := range tests {
+			t.Run(test.name, func(t *testing.T) {
+				val := Just(test.elements...).Min(func(a, b any) bool {
+					return a.(int) < b.(int)
+				})
+				assetEqual(t, test.min, val)
+			})
+		}
+	})
+}
+
 func BenchmarkParallelMapReduce(b *testing.B) {
 	b.ReportAllocs()