From b106c428aea7ef40479c3afa1845ae99e020094e Mon Sep 17 00:00:00 2001 From: donutloop Date: Wed, 29 Dec 2021 02:51:50 +0100 Subject: [PATCH] Every: use int counter (#6) Use counter to verify all elements passed the perdicate func. Replace slice of indexes with int counter. --- slice/slice.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index 388ee20..17ce421 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -100,15 +100,15 @@ func Every(slice, function interface{}) bool { panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String()) } - var indexes []int + var currentLength int for i := 0; i < sv.Len(); i++ { flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0] if flag.Bool() { - indexes = append(indexes, i) + currentLength++ } } - return len(indexes) == sv.Len() + return currentLength == sv.Len() } // Some return true if any of the values in the list pass the predicate function.