Documentation
¶
Overview ¶
Package vtools implements various utility types and functions for day to day use. 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 All[T comparable](s []T, f func(T) bool) bool
- func Any[T any](s []T, f func(T) bool) bool
- func Assert(b bool, msg ...any)
- func AtoiOrPanic(s string) int
- func Count[T comparable](s []T, target T) int
- func CountFunc[T any](s []T, shouldCount func(T) bool) int
- func Counter[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 []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 []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 Permute[S ~[]T, T any](s S) iter.Seq[S]
- func Range(vs ...int) iter.Seq[int]
- func ReadFileLines(fPath string) ([]string, error)
- func ReadFileLinesBytes(fPath string) ([][]byte, error)
- func ReadLines(r io.Reader) ([]string, error)
- func ReadLinesBytes(r io.Reader) ([][]byte, error)
- func SetValues[T any](s []T, t T)
- func StrBytes(s string) iter.Seq[byte]
- func Sum[T Number](s []T) T
- func TimeIt(start time.Time, action string)
- func Zip[T1 any, T2 any](s1 []T1, s2 []T2) iter.Seq2[T1, T2]
- type Number
- type Queue
- type Set
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All[T comparable](s []T, f func(T) bool) bool
All returns whether all f(v) returns true for all v in s.
func AtoiOrPanic ¶
AtoiOrPanic returns string s converted to an integer, or panics on conversion error.
func Count ¶
func Count[T comparable](s []T, target T) int
Count returns the count of elements in s equal to target.
func CountFunc ¶
CountFunc returns the count of items in s for which shouldCount(item) returns true.
func Counter ¶
func Counter[T comparable](s []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 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 Filter ¶
FilterSlice returns a slice containing only elements of s for which shouldKeep returns true.
func GCD ¶
func GCD[T constraints.Integer](a, b T) T
GCD returns the greatest common divisor of a and b.
func GCDAll ¶
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 ¶
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 ¶
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 ¶
NewSliceValues returns a new slice of the specified length with all values set to t.
func Permute ¶ added in v0.0.3
Permute returns a sequence of all permutations of s. The permutation order is not guaranteed to be lexicographic.
The input slice is not modified.
The backing slice of the sequence is the same each iteration. It should not be modified or kept between iterations. Use slices.Clone to acquire a copy for your own use.
func Range ¶
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 ReadFileLines ¶
ReadFileLines reads file fPath and returns all the non-empty lines.
func ReadFileLinesBytes ¶
ReadFileLinesBytes returns all the lines of file fPath as bytes.
func ReadLinesBytes ¶
ReadLinesBytes returns all the non-empty lines read from r. This func uses a bufio.Reader so providing a buffered reader is unnecessary.
Types ¶
type Number ¶
type Number interface {
constraints.Integer | constraints.Float
}
Number is a constraint that contains all number types.
type Queue ¶
type Queue[T any] []T
Queue is a generic queue data structure. Its zero-value is ready to use.
func NewQueue ¶
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 ¶
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.
func (Set[T]) Del ¶
func (s Set[T]) Del(ts ...T)
Del removes all the provided elements from the set. Del does nothing when called with an item that does not exist in the set.
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.