github.com/tanqiangyes/govalidator@v0.0.5/arrays.go (about) 1 package govalidator 2 3 // Iterator is the function that accepts element of slice/array and its index 4 type Iterator[T any] func(T, int) 5 6 // ResultIterator is the function that accepts element of slice/array and its index and returns any result 7 type ResultIterator[T any] func(T, int) T 8 9 // ConditionIterator is the function that accepts element of slice/array and its index and returns boolean 10 type ConditionIterator[T any] func(T, int) bool 11 12 // ReduceIterator is the function that accepts two element of slice/array and returns result of merging those values 13 type ReduceIterator[T any] func(T, T) T 14 15 // Some validates that any item of array corresponds to ConditionIterator. Returns boolean. 16 func Some[T any](array []T, iterator ConditionIterator[T]) bool { 17 res := false 18 for index, data := range array { 19 res = res || iterator(data, index) 20 } 21 return res 22 } 23 24 // Every validates that every item of array corresponds to ConditionIterator. Returns boolean. 25 func Every[T any](array []T, iterator ConditionIterator[T]) bool { 26 res := true 27 for index, data := range array { 28 res = res && iterator(data, index) 29 } 30 return res 31 } 32 33 // Reduce boils down a list of values into a single value by ReduceIterator 34 func Reduce[T any](array []T, iterator ReduceIterator[T], initialValue T) T { 35 for _, data := range array { 36 initialValue = iterator(initialValue, data) 37 } 38 return initialValue 39 } 40 41 // Each iterates over the slice and apply Iterator to every item 42 func Each[T any](array []T, iterator Iterator[T]) { 43 for index, data := range array { 44 iterator(data, index) 45 } 46 } 47 48 // Map iterates over the slice and apply ResultIterator to every item. Returns new slice as a result. 49 func Map[T any](array []T, iterator ResultIterator[T]) []T { 50 var result = make([]T, len(array)) 51 for index, data := range array { 52 result[index] = iterator(data, index) 53 } 54 return result 55 } 56 57 // Find iterates over the slice and apply ConditionIterator to every item. Returns first item that meet ConditionIterator or nil otherwise. 58 func Find[T any](array []T, iterator ConditionIterator[T]) (t T) { 59 for index, data := range array { 60 if iterator(data, index) { 61 return data 62 } 63 } 64 return 65 } 66 67 // Filter iterates over the slice and apply ConditionIterator to every item. Returns new slice. 68 func Filter[T any](array []T, iterator ConditionIterator[T]) []T { 69 var result = make([]T, 0) 70 for index, data := range array { 71 if iterator(data, index) { 72 result = append(result, data) 73 } 74 } 75 return result 76 } 77 78 // Count iterates over the slice and apply ConditionIterator to every item. Returns count of items that meets ConditionIterator. 79 func Count[T any](array []T, iterator ConditionIterator[T]) int { 80 count := 0 81 for index, data := range array { 82 if iterator(data, index) { 83 count = count + 1 84 } 85 } 86 return count 87 }