39 lines
640 B
Go
39 lines
640 B
Go
package utils
|
|
|
|
type ObjectType []interface{}
|
|
|
|
func ForEach[T any](arr *[]T, fun func(item *T)) {
|
|
for i := range *arr {
|
|
fun(&((*arr)[i]))
|
|
}
|
|
}
|
|
|
|
func Map[T any, V any](arr []T, fun func(item T) V) []V {
|
|
res := make([]V, 0, len(arr))
|
|
for _, item := range arr {
|
|
res = append(res, fun(item))
|
|
}
|
|
return res
|
|
}
|
|
|
|
func Distinct[T comparable](arr []T) []T {
|
|
set := map[T]interface{}{}
|
|
for _, item := range arr {
|
|
set[item] = nil
|
|
}
|
|
var res []T
|
|
for k := range set {
|
|
res = append(res, k)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func Contains[T comparable](arr []T, item T) bool {
|
|
for _, a := range arr {
|
|
if a == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|