initial commit
This commit is contained in:
36
backend/pkg/slices/map.go
Normal file
36
backend/pkg/slices/map.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user