mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-29 16:02:51 -05:00
core: add slices, paths, exec utils
This commit is contained in:
56
core/internal/utils/slices.go
Normal file
56
core/internal/utils/slices.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package utils
|
||||
|
||||
func Filter[T any](items []T, predicate func(T) bool) []T {
|
||||
var result []T
|
||||
for _, item := range items {
|
||||
if predicate(item) {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func Find[T any](items []T, predicate func(T) bool) (T, bool) {
|
||||
for _, item := range items {
|
||||
if predicate(item) {
|
||||
return item, true
|
||||
}
|
||||
}
|
||||
var zero T
|
||||
return zero, false
|
||||
}
|
||||
|
||||
func Map[T, U any](items []T, transform func(T) U) []U {
|
||||
result := make([]U, len(items))
|
||||
for i, item := range items {
|
||||
result[i] = transform(item)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func Contains[T comparable](items []T, target T) bool {
|
||||
for _, item := range items {
|
||||
if item == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Any[T any](items []T, predicate func(T) bool) bool {
|
||||
for _, item := range items {
|
||||
if predicate(item) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func All[T any](items []T, predicate func(T) bool) bool {
|
||||
for _, item := range items {
|
||||
if !predicate(item) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user