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

refactor todo file generation

This commit is contained in:
Jesse Duffield
2022-03-20 16:19:27 +11:00
parent 99e55725fb
commit 43d3f2bcb6
6 changed files with 91 additions and 40 deletions

View File

@ -256,6 +256,7 @@ func Remove[T any](slice []T, index int) []T {
return slices.Delete(slice, index, index+1)
}
// Removes the element at the 'fromIndex' and then inserts it at 'toIndex'.
// Operates on the input slice. Expected use is to reassign the result to the input slice.
func Move[T any](slice []T, fromIndex int, toIndex int) []T {
item := slice[fromIndex]
@ -263,6 +264,12 @@ func Move[T any](slice []T, fromIndex int, toIndex int) []T {
return slices.Insert(slice, toIndex, item)
}
// Swaps two elements at the given indices.
// Operates on the input slice.
func Swap[T any](slice []T, index1 int, index2 int) {
slice[index1], slice[index2] = slice[index2], slice[index1]
}
// Similar to Append but we leave the original slice untouched and return a new slice
func Concat[T any](slice []T, values ...T) []T {
newSlice := make([]T, 0, len(slice)+len(values))
@ -346,6 +353,17 @@ func Find[T any](slice []T, f func(T) bool) (T, bool) {
return zero[T](), false
}
// Sometimes you need to find an element and then map it to some other value based on
// information you obtained while finding it. This function lets you do that
func FindMap[T any, V any](slice []T, f func(T) (V, bool)) (V, bool) {
for _, element := range slice {
if value, ok := f(element); ok {
return value, true
}
}
return zero[V](), false
}
func ForEach[T any](slice []T, f func(T)) {
for _, element := range slice {
f(element)
@ -376,6 +394,14 @@ func TryForEachWithIndex[T any](slice []T, f func(T, int) error) error {
return nil
}
func Sum[T constraints.Ordered](i []T) T {
sum := zero[T]()
for _, value := range i {
sum += value
}
return sum
}
func zero[T any]() T {
var value T
return value