From ab6fec2f6942a809860ae4f21aa9950f6a08d829 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sun, 27 Nov 2022 21:36:30 +0800 Subject: [PATCH] refactor: fix bad code smell --- slice/slice.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index c9ecd8f..0adfcbf 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -170,33 +170,29 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo // Every return true if all of the values in the slice pass the predicate function. func Every[T any](slice []T, predicate func(index int, item T) bool) bool { - var currentLength int - for i, v := range slice { - if predicate(i, v) { - currentLength++ + if !predicate(i, v) { + return false } } - return currentLength == len(slice) + return true } // None return true if all the values in the slice mismatch the criteria func None[T any](slice []T, predicate func(index int, item T) bool) bool { - - var currentLength int + l := 0 for i, v := range slice { if !predicate(i, v) { - currentLength++ + l++ } } - return currentLength == len(slice) + return l == len(slice) } // Some return true if any of the values in the list pass the predicate function. func Some[T any](slice []T, predicate func(index int, item T) bool) bool { - for i, v := range slice { if predicate(i, v) { return true