Renamed unique_append to uniqueAppend
This commit is contained in:
@@ -50,7 +50,7 @@ func isNormalChar(c rune) bool {
|
|||||||
|
|
||||||
// Ensure that the given elements are only appended to the given slice if they
|
// Ensure that the given elements are only appended to the given slice if they
|
||||||
// don't already exist. Returns the new slice, and the number of unique items appended.
|
// don't already exist. Returns the new slice, and the number of unique items appended.
|
||||||
func unique_append[T comparable](slc []T, items ...T) ([]T, int) {
|
func uniqueAppend[T comparable](slc []T, items ...T) ([]T, int) {
|
||||||
num_appended := 0
|
num_appended := 0
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if !slices.Contains(slc, item) {
|
if !slices.Contains(slc, item) {
|
||||||
@@ -61,6 +61,25 @@ func unique_append[T comparable](slc []T, items ...T) ([]T, int) {
|
|||||||
return slc, num_appended
|
return slc, num_appended
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func uniqueAppendFunc[T any](slc []T, fn func(T, T) bool, items ...T) ([]T, int) {
|
||||||
|
toRet := make([]T, len(slc))
|
||||||
|
num_appended := 0
|
||||||
|
copy(toRet, slc)
|
||||||
|
for _, item := range items {
|
||||||
|
itemExists := false
|
||||||
|
for _, val := range slc {
|
||||||
|
if fn(item, val) {
|
||||||
|
itemExists = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !itemExists {
|
||||||
|
toRet = append(toRet, item)
|
||||||
|
num_appended++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toRet, num_appended
|
||||||
|
}
|
||||||
|
|
||||||
// Returns true only if all the given elements are equal
|
// Returns true only if all the given elements are equal
|
||||||
func allEqual[T comparable](items ...T) bool {
|
func allEqual[T comparable](items ...T) bool {
|
||||||
first := items[0]
|
first := items[0]
|
||||||
|
Reference in New Issue
Block a user