37 lines
753 B
Go
37 lines
753 B
Go
package slices
|
|
|
|
// Map consumes []T and a function that returns D
|
|
func Map[T, D any](srcItems []T, mapFunc func(T) D) []D {
|
|
dstItems := make([]D, len(srcItems))
|
|
for i, v := range srcItems {
|
|
dstItems[i] = mapFunc(v)
|
|
}
|
|
return dstItems
|
|
}
|
|
|
|
// First returns the first of a slice
|
|
func First[T any](s []T) (item T, found bool) {
|
|
if len(s) > 0 {
|
|
return s[0], true
|
|
}
|
|
return item, false
|
|
}
|
|
|
|
// Last returns the last of a slice
|
|
func Last[T any](s []T) (item T, found bool) {
|
|
if len(s) > 0 {
|
|
return s[len(s)-1], true
|
|
}
|
|
return item, false
|
|
}
|
|
|
|
// FindFirst finds the first matching item in s given fn
|
|
func FindFirst[T any](s []T, fn func(T) bool) (item T, found bool) {
|
|
for _, v := range s {
|
|
if fn(v) {
|
|
return v, true
|
|
}
|
|
}
|
|
return item, false
|
|
}
|