From 83c069e2349cb96c505a1aab53e91286e8e2975b Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 24 Jun 2025 19:14:37 +0800 Subject: [PATCH 1/2] Update slice_concurrent.go --- slice/slice_concurrent.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slice/slice_concurrent.go b/slice/slice_concurrent.go index b49b55a..2cd0d9b 100644 --- a/slice/slice_concurrent.go +++ b/slice/slice_concurrent.go @@ -125,6 +125,8 @@ func ReduceConcurrent[T any](slice []T, initial T, reducer func(index int, item func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T { result := make([]T, 0) var wg sync.WaitGroup + var mu sync.Mutex + workerChan := make(chan struct{}, numThreads) @@ -137,7 +139,9 @@ func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, defer wg.Done() if predicate(i, v) { + mu.Lock() result = append(result, v) + mu.Unlock() } <-workerChan From 93c777a4186993738cd0b2d8d7356c9a7d26a27a Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 22 Sep 2025 10:04:23 +0800 Subject: [PATCH 2/2] Update mathutil.go fix: return 0 when Average is called with empty slice --- mathutil/mathutil.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index eac0239..c617360 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -238,6 +238,9 @@ func Sum[T constraints.Integer | constraints.Float](numbers ...T) T { // Average return average value of numbers. // Play: https://go.dev/play/p/Vv7LBwER-pz func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 { + if len(numbers) == 0 { + return 0 + } var sum float64 for _, num := range numbers { sum += float64(num)