Added 'unique append' function, to ensure that elements in slice are unique
This commit is contained in:
12
misc.go
12
misc.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,3 +14,14 @@ func assert(cond bool) {
|
|||||||
panic("Assertion Failed")
|
panic("Assertion Failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that the given elements are only appended to the given slice if they
|
||||||
|
// don't already exist.
|
||||||
|
func unique_append[T comparable](slc []T, items ...T) []T {
|
||||||
|
for _, item := range items {
|
||||||
|
if !slices.Contains(slc, item) {
|
||||||
|
slc = append(slc, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slc
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user