vtools

package module
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 8 Imported by: 35

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs added in v0.0.10

func Abs[T Number](a T) T

Abs returns the absolute value of number a.

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

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

AllSliceFunc returns true if f(item) returns true for all items in s, otherwise false.

func Any

func Any[T any](seq iter.Seq[T], f func(T) bool) bool

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

func AnySlice

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

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

func AtoiOrPanic added in v0.0.13

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 iter.Seq[T], target T) int

Count returns the number of items in s that are 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 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

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 iter.Seq[T], shouldKeep func(T) bool) iter.Seq[T]

Filter returns a sequence of items from s for which shouldKeep(item) returns true.

func FilterSlice

func FilterSlice[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 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 Map

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

Map maps the items in s to a new sequence by calling to(s)

func MapSlice

func MapSlice[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 added in v0.0.14

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 Range added in v0.0.12

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 ReadLines

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

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

func ReadLinesBytes added in v0.0.18

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

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.

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 iter.Seq[T]) T

Sum returns the sum of a sequence of a numbers.

func SumSlice

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

SumSlice 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")

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

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 added in v0.0.11

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 added in v0.0.11

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 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.

func (Set[T]) Add

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

Add adds 1 or more items to the set.

func (Set[T]) Contains

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

Contains returns whether t exists in the set.

func (Set[T]) Delete

func (s Set[T]) Delete(t T)

Delete removes t from the set. Delete 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

func NewStack[T any](stackCap 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