Documentation ¶
Overview ¶
Package cuts provides useful, generic utilities for working with slices.
Index ¶
- Variables
- func AllWhere[S ~[]E, E any](vals S, where func(val E) bool) bool
- func AnyWhere[S ~[]E, E any](vals S, where func(val E) bool) bool
- func ChunkBy[T any](items []T, chunkSize int) (chunks [][]T)
- func Dedupe[T comparable](in []T) []T
- func FirstWhere[S ~[]E, E cmp.Ordered](vals S, where func(val E) bool) (int, bool)
- func LastWhere[S ~[]E, E cmp.Ordered](vals S, where func(val E) bool) (int, bool)
- func SnapTo[S ~[]E, E constraints.Integer | constraints.Float](vals S, target E) E
- func SnapToFunc[S ~[]E, E any](vals S, target E, cmp func(E, E) int, closest func(tgt, nxt, prv E) E) E
- func SnapToStr[S ~[]E, E ~string](vals S, target E) E
- func SnapToStrFunc[S ~[]E, E ~string](vals S, target E, matcher FuzzyMatcher[E]) E
- type BagOfWordsMatcherOpt
- type FZFV1MatcherOpt
- type FZFV2MatcherOpt
- type FuzzyMatcher
Constants ¶
This section is empty.
Variables ¶
var DefaultBagSizes = []int{2, 3, 4}
DefaultBagSizes are the default bag sizes uses by BagOfWordsMatcher, if none are specified.
Functions ¶
func Dedupe ¶
func Dedupe[T comparable](in []T) []T
Dedupe removes duplicate values from an array of comparable elements.
func FirstWhere ¶
FirstWhere searches for the first element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.
func LastWhere ¶
LastWhere searches for the last element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.
func SnapTo ¶
func SnapTo[S ~[]E, E constraints.Integer | constraints.Float](vals S, target E) E
SnapTo returns the element in an array that is closest to the target.
func SnapToFunc ¶
func SnapToFunc[S ~[]E, E any](vals S, target E, cmp func(E, E) int, closest func(tgt, nxt, prv E) E) E
SnapToFunc returns the element in an array that is closest to the target, given a custom comparison and closest function.
func SnapToStr ¶
func SnapToStr[S ~[]E, E ~string](vals S, target E) E
SnapToStr returns the closest element in a string array to the target. It uses the Levenshtein distance implementation from github.com/lithammer/fuzzysearch/fuzzy to calculate closeness.
func SnapToStrFunc ¶
func SnapToStrFunc[S ~[]E, E ~string](vals S, target E, matcher FuzzyMatcher[E]) E
SnapToStrFunc returns the closest element in a string array to the target, given a provided fuzzy matcher.
Types ¶
type BagOfWordsMatcherOpt ¶
type BagOfWordsMatcherOpt[T ~string] interface { // contains filtered or unexported methods }
BagOfWordsMatcherOpt configures a BagOfWordsMatcher
func WithBagSizes ¶
func WithBagSizes[T ~string](b []int) BagOfWordsMatcherOpt[T]
WithBagSizes configures the bag sizes of BagOfWordsMatcher
type FZFV1MatcherOpt ¶
type FZFV1MatcherOpt[T ~string] interface { // contains filtered or unexported methods }
func CaseSensitiveV1 ¶
func CaseSensitiveV1[T ~string]() FZFV1MatcherOpt[T]
CaseSensitiveV1 configures a FZFV1Matcher to be case-sensitive.
func ForwardV1 ¶
func ForwardV1[T ~string]() FZFV1MatcherOpt[T]
ForwardV1 configures a FZFV1Matcher to look forward.
func NormalizeV1 ¶
func NormalizeV1[T ~string]() FZFV1MatcherOpt[T]
NormalizeV1 configures a FZFV1Matcher to normalize strings.
func WithSlabV1 ¶
func WithSlabV1[T ~string](slab *util.Slab) FZFV1MatcherOpt[T]
WithSlabV1 configures a FZFV1Matcher to use a custom Slab.
type FZFV2MatcherOpt ¶
type FZFV2MatcherOpt[T ~string] interface { // contains filtered or unexported methods }
FZFV2MatcherOpt configures a FZFV2Matcher.
func CaseSensitiveV2 ¶
func CaseSensitiveV2[T ~string]() FZFV2MatcherOpt[T]
CaseSensitiveV2 configures a FZFV2Matcher to be case-sensitive.
func ForwardV2 ¶
func ForwardV2[T ~string]() FZFV2MatcherOpt[T]
ForwardV2 configures a FZFV2Matcher to look forward.
func NormalizeV2 ¶
func NormalizeV2[T ~string]() FZFV2MatcherOpt[T]
NormalizeV2 configures a FZFV2Matcher to normalize strings.
func WithSlabV2 ¶
func WithSlabV2[T ~string](slab *util.Slab) FZFV2MatcherOpt[T]
WithSlabV2 configures a FZFV2Matcher to use a custom Slab.
type FuzzyMatcher ¶
type FuzzyMatcher[T ~string] interface { Closest(target, next, prev T) T }
FuzzyMatcher is an interface for various implementations of fuzzy string matching.
func BagOfWordsMatcher ¶
func BagOfWordsMatcher[T ~string](opts ...BagOfWordsMatcherOpt[T]) FuzzyMatcher[T]
BagOfWordsMatcher is a FuzzyMatcher that uses a bag-of-words approach to calculate closeness, as implemented by github.com/schollz/closestmatch.
func FZFV1Matcher ¶
func FZFV1Matcher[T ~string](opts ...FZFV1MatcherOpt[T]) FuzzyMatcher[T]
FZFV1Matcher is a FuzzyMatcher using fzf's V1 algorithm. The V2 algorithm is faster than the fzf's V2 algorithm, but provides lower quality matches.
See github.com/junegunn/fzf/src/algo for more information.
func FZFV2Matcher ¶
func FZFV2Matcher[T ~string](opts ...FZFV2MatcherOpt[T]) FuzzyMatcher[T]
FZFV2Matcher is a FuzzyMatcher using fzf's V2 algorithm. The V2 algorithm is slower than the fzf's V1 algorithm, but provides higher quality matches.
See github.com/junegunn/fzf/src/algo for more information.
func LevenshteinMatcher ¶
func LevenshteinMatcher[T ~string]() FuzzyMatcher[T]
LevenshteinMatcher is a FuzzyMatcher that uses Levenshtein distance to calculate closeness, as implemented by github.com/lithammer/fuzzysearch/fuzzy.