Files
kserve/pkg/utils/collections.go
112a2c7632 feat: improves collection helpers (#4579)
Signed-off-by: Bartosz Majsak <[email protected]>
Co-authored-by: Sivanantham <[email protected]>
2025-07-08 18:44:49 +05:30

76 lines
1.5 KiB
Go

/*
Copyright 2025 The KServe Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"strings"
)
type PredicateFn[T any] func(T) bool
func FilterSlice[T any](items []T, predicate PredicateFn[T]) []T {
if len(items) == 0 {
return []T{}
}
result := make([]T, 0, len(items))
for _, item := range items {
if predicate(item) {
result = append(result, item)
}
}
return result
}
func Includes[T comparable](slice []T, value T) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
func IncludesArg(slice []string, arg string) bool {
for _, v := range slice {
if v == arg || strings.HasPrefix(v, arg) {
return true
}
}
return false
}
func Filter[K comparable, V any](origin map[K]V, predicate PredicateFn[K]) map[K]V {
result := make(map[K]V)
for k, v := range origin {
if predicate(k) {
result[k] = v
}
}
return result
}
func Union[K comparable, V any](maps ...map[K]V) map[K]V {
result := make(map[K]V)
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}