1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

make more use of generics

This commit is contained in:
Jesse Duffield
2022-03-19 12:26:30 +11:00
parent dde30fa104
commit c7a629c440
52 changed files with 3013 additions and 274 deletions

View File

@ -1,29 +1,5 @@
package utils
// IncludesString if the list contains the string
func IncludesString(list []string, a string) bool {
return IncludesStringFunc(list, func(b string) bool { return b == a })
}
func IncludesStringFunc(list []string, fn func(string) bool) bool {
for _, b := range list {
if fn(b) {
return true
}
}
return false
}
// IncludesInt if the list contains the Int
func IncludesInt(list []int, a int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// NextIndex returns the index of the element that comes after the given number
func NextIndex(numbers []int, currentNumber int) int {
for index, number := range numbers {
@ -45,44 +21,6 @@ func PrevIndex(numbers []int, currentNumber int) int {
return 0
}
// UnionInt returns the union of two int arrays
func UnionInt(a, b []int) []int {
m := make(map[int]bool)
for _, item := range a {
m[item] = true
}
for _, item := range b {
if _, ok := m[item]; !ok {
// this does not mutate the original a slice
// though it does mutate the backing array I believe
// but that doesn't matter because if you later want to append to the
// original a it must see that the backing array has been changed
// and create a new one
a = append(a, item)
}
}
return a
}
// DifferenceInt returns the difference of two int arrays
func DifferenceInt(a, b []int) []int {
result := []int{}
m := make(map[int]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
result = append(result, item)
}
}
return result
}
// NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end
func NextIntInCycle(sl []int, current int) int {
for i, val := range sl {
@ -121,19 +59,6 @@ func StringArraysOverlap(strArrA []string, strArrB []string) bool {
return false
}
func Uniq(values []string) []string {
added := make(map[string]bool)
result := make([]string, 0, len(values))
for _, value := range values {
if added[value] {
continue
}
added[value] = true
result = append(result, value)
}
return result
}
func Limit(values []string, limit int) []string {
if len(values) > limit {
return values[:limit]
@ -141,14 +66,6 @@ func Limit(values []string, limit int) []string {
return values
}
func Reverse(values []string) []string {
result := make([]string, len(values))
for i, val := range values {
result[len(values)-i-1] = val
}
return result
}
func LimitStr(value string, limit int) string {
n := 0
for i := range value {