2 Commits

Author SHA1 Message Date
2124bff9de added Search function 2022-08-25 14:03:50 +02:00
2fb5ea542d fixed reverse implementation 2022-08-21 14:08:25 +02:00
2 changed files with 12 additions and 1 deletions

View File

@ -4,7 +4,7 @@ func Reverse[T any](slice []T) []T {
s := make([]T, len(slice)) s := make([]T, len(slice))
for i := 0; i < len(slice); i++ { for i := 0; i < len(slice); i++ {
ri := len(slice) - 1 - i ri := len(slice) - 1 - i
s[ri] = s[i] s[ri] = slice[i]
} }
return s return s
} }

11
search.go Normal file
View File

@ -0,0 +1,11 @@
package slices
func Search[T any](slice []T, f func(a, b T) T) T {
if len(slice) == 0 {
return *new(T)
}
value := slice[0]
Each(slice, func(v T) { value = f(value, v) })
return value
}