1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

would you believe that I'm adding even more generics

This commit is contained in:
Jesse Duffield
2022-03-19 19:51:48 +11:00
parent 1b75ed3740
commit 94a53484a1
14 changed files with 62 additions and 51 deletions

View File

@ -37,6 +37,16 @@ func Map[T any, V any](slice []T, f func(T) V) []V {
return result
}
// Produces a new slice, leaves the input slice untouched.
func MapWithIndex[T any, V any](slice []T, f func(T, int) V) []V {
result := make([]V, 0, len(slice))
for i, value := range slice {
result = append(result, f(value, i))
}
return result
}
// Produces a new slice, leaves the input slice untouched.
func FlatMap[T any, V any](slice []T, f func(T) []V) []V {
// impossible to know how long this slice will be in the end but the length
@ -74,6 +84,18 @@ func Filter[T any](slice []T, test func(T) bool) []T {
return result
}
// Produces a new slice, leaves the input slice untouched.
func FilterWithIndex[T any](slice []T, f func(T, int) bool) []T {
result := make([]T, 0, len(slice))
for i, value := range slice {
if f(value, i) {
result = append(result, value)
}
}
return result
}
// Mutates original slice. Intended usage is to reassign the slice result to the input slice.
func FilterInPlace[T any](slice []T, test func(T) bool) []T {
newLength := 0