vtools

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Number](a T) T

Abs returns the absolute value of number a.

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 Any

func Any[T any](s []T, f func(T) bool) bool

Any returns true if f(item) is true for at least one item in s, otherwise false.

func Assert

func Assert(b bool, msg ...any)

func AtoiOrPanic

func AtoiOrPanic(s string) int

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

func CountFunc[T any](s []T, shouldCount func(T) bool) int

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

func Cycle[T any](s []T) iter.Seq[T]

Cycle returns an iterator that endlessly loops over s. Analogous to python's itertools.cycle.

func Enumerate

func Enumerate[T any](s iter.Seq[T]) iter.Seq2[int, T]

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

func Filter[T any](s []T, shouldKeep func(T) bool) []T

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 Map

func Map[T any, E any](s []T, to func(T) E) []E

MapSlice returns a slice of the items in s with to(item) called on each one.

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

func NewSliceValues[T any](length int, t T) []T

NewSliceValues returns a new slice of the specified length with all values set to t.

func Permute added in v0.0.3

func Permute[S ~[]T, T any](s S) iter.Seq[S]

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

func Range(vs ...int) iter.Seq[int]

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

func ReadFileLines(fPath string) ([]string, error)

ReadFileLines reads file fPath and returns all the non-empty lines.

func ReadFileLinesBytes

func ReadFileLinesBytes(fPath string) ([][]byte, error)

ReadFileLinesBytes returns all the lines of file fPath as bytes.

func ReadLines

func ReadLines(r io.Reader) ([]string, error)

ReadLines reads all the non-empty lines from r.

func ReadLinesBytes

func ReadLinesBytes(r io.Reader) ([][]byte, error)

ReadLinesBytes returns all the non-empty lines read from r. This func uses a bufio.Reader so providing a buffered reader is unnecessary.

func SetValues

func SetValues[T any](s []T, t T)

SetValues sets each index of s to t.

func StrBytes

func StrBytes(s string) iter.Seq[byte]

StrBytes returns an iterator over the bytes in s.

func Sum

func Sum[T Number](s []T) T

Sum returns the sum of a slice of numbers.

func TimeIt

func TimeIt(start time.Time, action string)

TimeIt prints to stdout the time some action took. Intended usage is defer TimeIt(time.Now(), "foo")

func Zip added in v0.0.6

func Zip[T1 any, T2 any](s1 []T1, s2 []T2) iter.Seq2[T1, T2]

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

func NewQueue[T any](cap int) Queue[T]

NewQueue returns a queue with an initialized capacity. Use of cap prevents later memory allocations.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() T

Pop removes and returns the item at the front of the queue. Pop panics if called on an empty queue.

func (*Queue[T]) Push

func (q *Queue[T]) Push(items ...T)

Push pushes items to the back of the queue.

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]) Add

func (s Set[T]) Add(items ...T)

Add adds 1 or more items to the set.

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.

func (Set[T]) Equals added in v0.0.5

func (s Set[T]) Equals(other Set[T]) bool

Equals returns whether the contents of other are the same as s.

func (Set[T]) Has

func (s Set[T]) Has(t T) bool

Has returns whether t exists 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

func NewStack[T any](capacity int) Stack[T]

NewStack returns a Stack with the specified capacity. This capacity may prevent memory allocations when working with the stack.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() T

Pop removes the item at the top of the stack and returns it. Pop panics if called on an empty stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(t ...T)

Push puts t at the top of the stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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