From f3bf5e974007b565b03d09f42e1c355e0c3f8c44 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 31 Oct 2024 17:09:25 -0400 Subject: [PATCH] Added function to check for word boundaries and delete an element from a slice --- misc.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/misc.go b/misc.go index 648131a..31a3cfb 100644 --- a/misc.go +++ b/misc.go @@ -21,6 +21,15 @@ func dotChars() []rune { // Returns all possible characters represented by the d return to_return } +// Returns true if str[idx] and str[idx-1] are separated by a word boundary. +func isWordBoundary(str []rune, idx int) bool { + wbounded := idx == 0 || + idx == len(str)-1 || + (!slices.Contains(wordChars, str[idx-1]) && slices.Contains(wordChars, str[idx])) || + (slices.Contains(wordChars, str[idx-1]) && !slices.Contains(wordChars, str[idx])) + return wbounded +} + func isAlphaNum(c rune) bool { return unicode.IsLetter(c) || unicode.IsNumber(c) } @@ -31,6 +40,16 @@ func assert(cond bool) { } } +func deleteFromSlice[T comparable](slc []T, val T) []T { + toReturn := make([]T, 0) + for _, v := range slc { + if v != val { + toReturn = append(toReturn, v) + } + } + return toReturn +} + // 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. func unique_append[T comparable](slc []T, items ...T) ([]T, int) {