refactor: push commands in to cmd and ev to root as library
This commit is contained in:
35
pkg/slice/slice.go
Normal file
35
pkg/slice/slice.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package slice
|
||||
|
||||
// FilterType returns a subset that matches the type.
|
||||
func FilterType[T any](in ...any) []T {
|
||||
lis := make([]T, 0, len(in))
|
||||
for _, u := range in {
|
||||
if t, ok := u.(T); ok {
|
||||
lis = append(lis, t)
|
||||
}
|
||||
}
|
||||
return lis
|
||||
}
|
||||
|
||||
// Find returns the first of type found. or false if not found.
|
||||
func Find[T any](in ...any) (T, bool) {
|
||||
return First(FilterType[T](in...)...)
|
||||
}
|
||||
|
||||
// First returns the first element in a slice.
|
||||
func First[T any](in ...T) (T, bool) {
|
||||
if len(in) == 0 {
|
||||
var zero T
|
||||
return zero, false
|
||||
}
|
||||
return in[0], true
|
||||
}
|
||||
|
||||
// Map applys func to each element s and returns results as slice.
|
||||
func Map[T, U any](s []T, f func(T) U) []U {
|
||||
r := make([]U, len(s))
|
||||
for i, v := range s {
|
||||
r[i] = f(v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user