Documentation
¶
Overview ¶
Package vtools implements various utility types and functions for day to day use. These utilities are especially useful for writing puzzle-solving code, where errors do not need to be handled explicitly. These utilities include convenience functions for working with iterators and slices that are analogous to python's builtins and itertools.
Index ¶
- func Abs[T Number](a T) T
- func AllSlice[T comparable](s []T, target T) bool
- func AllSliceFunc[T any](s []T, f func(T) bool) bool
- func Any[T any](seq iter.Seq[T], f func(T) bool) bool
- func AnySlice[T any](s []T, f func(T) bool) bool
- func AtoiOrPanic(s string) int
- func Count[T comparable](s iter.Seq[T], target T) int
- func CountFunc[T any](s []T, shouldCount func(T) bool) int
- func CountSlice[T comparable](s []T, target T) int
- func Counter[T comparable](s iter.Seq[T]) map[T]int
- func CounterSlice[T comparable](s []T) map[T]int
- func Cycle[T any](s []T) iter.Seq[T]
- func Enumerate[T any](s iter.Seq[T]) iter.Seq2[int, T]
- func Filter[T any](s iter.Seq[T], shouldKeep func(T) bool) iter.Seq[T]
- func FilterSlice[T any](s []T, shouldKeep func(T) bool) []T
- func GCD[T constraints.Integer](a, b T) T
- func GCDAll[T constraints.Integer](nums ...T) T
- func LCM[T constraints.Integer](a, b T) T
- func LCMAll[T constraints.Integer](nums ...T) T
- func Map[T any, E any](s iter.Seq[T], to func(T) E) iter.Seq[E]
- func MapSlice[T any, E any](s []T, to func(T) E) []E
- func MaxIndex[T constraints.Ordered](s []T) (T, int)
- func NewSliceValues[T any](length int, t T) []T
- func Range(vs ...int) iter.Seq[int]
- func ReadLines(fPath string) ([]string, error)
- func ReadLinesBytes(fPath string) ([][]byte, error)
- func SetValues[T any](s []T, t T)
- func StrBytes(s string) iter.Seq[byte]
- func Sum[T Number](s iter.Seq[T]) T
- func SumSlice[T Number](s []T) T
- func TimeIt(start time.Time, action string)
- type Number
- type Queue
- type Set
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllSlice ¶ added in v0.0.6
func AllSlice[T comparable](s []T, target T) bool
AllSlice returns whether all items in s == target.
func AllSliceFunc ¶ added in v0.0.16
AllSliceFunc returns true if f(item) returns true for all items in s, otherwise false.
func AnySlice ¶
AnySlice returns true if f(item) is true for at least one item in s, otherwise false.
func AtoiOrPanic ¶ added in v0.0.13
AtoiOrPanic returns string s converted to an integer, or panics on conversion error.
func Count ¶
func Count[T comparable](s iter.Seq[T], target T) int
Count returns the number of items in s that are equal to target.
func CountFunc ¶
CountFunc returns the count of items in s for which shouldCount(item) returns true.
func CountSlice ¶
func CountSlice[T comparable](s []T, target T) int
CountSlice returns the count of elements in s equal to target.
func Counter ¶
func Counter[T comparable](s iter.Seq[T]) map[T]int
Counter returns a map whose keys are items in s and whose values are the counts of each item. e.g. Counter([]string{7, 1, 7, 9, 1, 3}) == map[string]int{1: 2, 7: 2, 3: 1, 9: 1}
func CounterSlice ¶
func CounterSlice[T comparable](s []T) map[T]int
CounterSlice returns a map whose keys are items in s and whose values are the counts of each item. e.g. Counter([]string{7, 1, 7, 9, 1, 3}) == map[string]int{1: 2, 7: 2, 3: 1, 9: 1}
func Cycle ¶
Cycle returns an iterator that endlessly loops over s. Analogous to python's itertools.cycle.
func Enumerate ¶
Enumerate returns an iterator where the 1st item in the pair is an index and the 2nd is the item in s. Analogous to python's enumerate.
func FilterSlice ¶
FilterSlice returns a slice containing only elements of s for which shouldKeep returns true.
func GCD ¶ added in v0.0.15
func GCD[T constraints.Integer](a, b T) T
GCD returns the greatest common divisor of a and b.
func GCDAll ¶ added in v0.0.15
func GCDAll[T constraints.Integer](nums ...T) T
GCDAll returns the greatest common divisor of all the integers provided. It panics if len(nums) == 0.
func LCM ¶ added in v0.0.15
func LCM[T constraints.Integer](a, b T) T
LCM returns the least common multiple of a and b. LCM(0, 0) = 0.
func LCMAll ¶ added in v0.0.15
func LCMAll[T constraints.Integer](nums ...T) T
LCMAll returns the least common multiple of the provided integers. It panics if len(nums) == 0
func MaxIndex ¶
func MaxIndex[T constraints.Ordered](s []T) (T, int)
MaxIndex returns the max value in s along with it's index. If there are multiple max value occurrences, the index of the first one is returned.
func NewSliceValues ¶ added in v0.0.14
NewSliceValues returns a new slice of the specified length with all values set to t.
func Range ¶ added in v0.0.12
Range returns an iterator over a range [low, high) with an optional step amount. Range takes either 2 or 3 arguments, an interval [low, high) and an increment step, it panics otherwise.
func ReadLinesBytes ¶ added in v0.0.18
ReadLinesBytes returns all the lines of file fPath as bytes.
func SetValues ¶ added in v0.0.14
func SetValues[T any](s []T, t T)
SetValues sets each index of s to t.
Types ¶
type Number ¶
type Number interface {
constraints.Integer | constraints.Float
}
Number is a constraint that contains all number types.
type Queue ¶ added in v0.0.11
type Queue[T any] []T
Queue is a generic queue data structure. Its zero-value is ready to use.
func NewQueue ¶ added in v0.0.11
NewQueue returns a queue with an initialized capacity. Use of cap prevents later memory allocations.
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a set data structure that keeps track of items. A zero-value Set is not ready to use, create one using NewSet.
func NewSet ¶
func NewSet[T comparable](capacity int) Set[T]
NewSet returns a Set with the specified capacity. The capacity may prevent allocations when working with the set.
func SetFromSlice ¶ added in v0.0.12
func SetFromSlice[T comparable](items []T) Set[T]
SetFromSlice returns a Set with all items in s present in the set. A utility to simplify set creation + initialization.
type Stack ¶
type Stack[T any] []T
Stack is a stack contianer. A zero value stack is ready to use.
func NewStack ¶
NewStack returns a Stack with the specified capacity. This capacity may prevent memory allocations when working with the stack.