Added fnunction to generate numbers in a range; added capacity to some slices to prevent unnecessary reallocations

master
Aadhavan Srinivasan 2 months ago
parent 8a69ea8cb7
commit e2e99ff6a9

@ -42,7 +42,7 @@ func assert(cond bool) {
} }
func deleteFromSlice[T comparable](slc []T, val T) []T { func deleteFromSlice[T comparable](slc []T, val T) []T {
toReturn := make([]T, 0) toReturn := make([]T, 0, len(slc))
for _, v := range slc { for _, v := range slc {
if v != val { if v != val {
toReturn = append(toReturn, v) toReturn = append(toReturn, v)
@ -77,7 +77,7 @@ func allEqual[T comparable](items ...T) bool {
// Returns all elements in slice A that are NOT in slice B // Returns all elements in slice A that are NOT in slice B
func setDifference[T comparable](s1 []T, s2 []T) []T { func setDifference[T comparable](s1 []T, s2 []T) []T {
toReturn := make([]T, 0) toReturn := make([]T, 0, len(s1))
for _, val := range s1 { for _, val := range s1 {
if !slices.Contains(s2, val) { if !slices.Contains(s2, val) {
toReturn = append(toReturn, val) toReturn = append(toReturn, val)
@ -110,3 +110,12 @@ func Reduce[T any](slc []T, fn func(T, T) T) T {
} }
return slc[0] return slc[0]
} }
// Generate numbers in a range - start (inclusive) to end (exclusive)
func genRange(start, end int) []int {
toRet := make([]int, end-start)
for i := start; i < end; i++ {
toRet[i-start] = i
}
return toRet
}

Loading…
Cancel
Save