cuts

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 8 Imported by: 0

README

cuts

GoDoc Build Status

Provides useful, generic utilities for working with slices.

Install

go get github.com/ejfrick/cuts

Usage

package main

import (
	"fmt"
	"github.com/ejfrick/cuts"
	"strings"
	"time"
)

func main() {
	// dedupe some values
	words := []string{"hello", "hello", "world"}
	dedupedWords := cuts.Dedupe(words)
	fmt.Println(dedupedWords) // ["hello", "world"]

	// chunk a slice into smaller slices
	bigList := []string{"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}
	chunks := cuts.ChunkBy(bigList, 2) // chunk into lists of two elements
	fmt.Println(chunks)                // [["the", "quick"], ["brown", "fox"], ["jumps", "over"], ["the", "lazy"], ["dog"]]

	nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}

	// get the first element that is even
	index, found := cuts.FirstWhere(nums, func(val int) bool { return val%2 == 0 })
	fmt.Println(index, found) // 2, true

	// get the last element that is odd
	index, found = cuts.LastWhere(nums, func(val int) bool { return val%2 == 1 })
	fmt.Println(index, found) // 7, true

	// get the last element that is greater than 10
	index, found = cuts.LastWhere(nums, func(val int) bool { return val > 10 })
	fmt.Println(index, found) // -1, false

	// check if any words in the slice contain the letter e
	ok := cuts.AnyWhere(bigList, func(val string) bool { return strings.Contains(val, "e") })
	fmt.Println(ok) // true

	// check if all words in the slice contain the letter e
	// fails fast!
	ok = cuts.AllWhere(bigList, func(val string) bool { return strings.Contains(val, "e") })
	fmt.Println(ok) // false
	
	// get closest value to target value
	acceptableTimes := []time.Duration{time.Hour, time.Hour * 6, time.Hour * 12}
	target := time.Hour * 7
	closest := cuts.SnapTo(acceptableTimes, target)
	fmt.Println(closest.String()) // 6h0m0s

}

See the cuts package documentation for more usage information.

License

MIT

Documentation

Overview

Package cuts provides useful, generic utilities for working with slices.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBagSizes = []int{2, 3, 4}

DefaultBagSizes are the default bag sizes uses by BagOfWordsMatcher, if none are specified.

Functions

func AllWhere

func AllWhere[S ~[]E, E any](vals S, where func(val E) bool) bool

AllWhere returns whether all elements in the slice satisfy the condition function.

func AnyWhere

func AnyWhere[S ~[]E, E any](vals S, where func(val E) bool) bool

AnyWhere returns whether any element in the slice satisfies the condition function.

func ChunkBy

func ChunkBy[T any](items []T, chunkSize int) (chunks [][]T)

ChunkBy groups an array of items into batches of the given size.

func Dedupe

func Dedupe[T comparable](in []T) []T

Dedupe removes duplicate values from an array of comparable elements.

func FirstWhere

func FirstWhere[S ~[]E, E cmp.Ordered](vals S, where func(val E) bool) (int, bool)

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

func LastWhere[S ~[]E, E cmp.Ordered](vals S, where func(val E) bool) (int, bool)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL