util

package
v0.0.0-...-d8be82f Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const MaxInt = int(MaxUint >> 1)
View Source
const MaxUint = ^uint(0)

Variables

This section is empty.

Functions

func All

func All[T any](slice []T, predicate func(T) bool) bool

All checks if all elements satisfy the predicate.

func Filter

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter is a simple filter over a slice.

func GroupString

func GroupString(s string) []string

GroupString returns the input string, broken into runs of consecutive characters

func KeyValuePairs

func KeyValuePairs(input string) map[string]string

KeyValuePairs splits a space-separated sequence of colon-separated key:value pairs into a map.

func LinesByParagraph

func LinesByParagraph(lines []string) [][]string

LinesByParagraph takes a slice of strings, and returns a slice of slices of strings: it separates paragraphs (multiple newlines).

func LowestTrue

func LowestTrue(lowFalseStart int, pred func(int) (bool, error)) (int, error)

func Map

func Map[T, U any](slice []T, mapper func(T) U) []U

Map is a simple map over a slice.

func MapE

func MapE[T, U any](slice []T, mapper func(T) (U, error)) ([]U, error)

MapE is a simple map over a slice, but passing errors up.

func MapSum

func MapSum[T Number](slicesOfItems [][]T) []T

MapSum returns a slice of the Sums of sublists.

func MappedSum

func MappedSum[T any, U constraints.Integer | constraints.Float | ~string](slice []T, mapper func(T) U) U

MappedSum runs `mapper` on each element, and sums the result.

func Max

func Max[T Number](items []T) T

Max gives the max of a slice of ints or floats.

func MustParseInts

func MustParseInts(commaString string, separator string) []int

MustParseInts parses a string of separated ints into a slice of ints, or panics.

func MustReadFileInts

func MustReadFileInts(filename string) []int

MustReadFileInts reads a file of ints, one per line, or panics.

func MustReadFileString

func MustReadFileString(filename string) string

MustReadFileString reads a string from a file or panics.

func MustReadLines

func MustReadLines(filename string) []string

MustReadLines reads a file and returns a slice of strings, one per line, or dies. MustReadFileString reads a string from a file or panics.

func MustReadSingleInt

func MustReadSingleInt(filename string) int

MustReadSingleInt reads a single integer from a file.

func MustStringsToInts

func MustStringsToInts(strings []string) []int

MustStringsToInts takes a slice of strings and returns a slice of ints, or panics.

func ParseFieldInts

func ParseFieldInts(s string) ([]int, error)

ParseFieldInts parses a string of ints separated by runs of whitespace into a slice of ints.

func ParseGrid

func ParseGrid(lines []string) ([][]int, error)

ParseGrid parses a set of lines of whitespacespace-separated ints into a 2D grid.

func ParseInts

func ParseInts(commaString string, separator string) ([]int, error)

ParseInts parses a string of separated ints into a slice of ints.

func ParseLinesOfInts

func ParseLinesOfInts(commaStrings []string, separator string) ([][]int, error)

ParseLinesOfInts runs ParseInts on every string in the passed slice, passing back a slice of slices of ints.

func ParseRegexInts

func ParseRegexInts(inputs []string, allowNegative bool) [][]int

ParseRegexInts takes a slice of lines of text. For each line, it searches for integers using a regex, and turns each into an integer. `allowNegative` determines whether a preceding minus sign is included in the regex or not.

func ReadFile

func ReadFile(filename string) ([]byte, error)

ReadFile is just os.ReadFile

func ReadFileInts

func ReadFileInts(filename string) ([]int, error)

ReadFileInts reads a file of ints, one per line

func ReadFileString

func ReadFileString(filename string) (string, error)

ReadFileString reads a file and returns it as a string, trimmed.

func ReadLines

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

ReadLines reads a file and returns a slice of strings, one per line.

func RemoveBlanks

func RemoveBlanks(ss []string) []string

RemoveBlanks returns a slice of strings, but trimmed, and with empty/all-whitespace strings removed.

func Reverse

func Reverse(s string) string

Reverse reverses a string.

func Split2

func Split2[T any](slice []T, predicate func(T) bool) ([]T, []T)

Split2 is a simple filter over a slice. It returns two slices: the first holding elements for which the predicate returned true, the second those for which it returned false.

func SplitAfter

func SplitAfter[T any](slice []T, predicate func(T) bool) [][]T

SplitAfter takes a slice and a predicate, and returns the slice chunked, split after any element where the predicate returns true.

func SplitBefore

func SplitBefore[T any](slice []T, predicate func(T) bool) [][]T

SplitBefore takes a slice and a predicate, and returns the slice chunked, split before any element where the predicate returns true.

func StringsToInt32s

func StringsToInt32s(strings []string) ([]int32, error)

StringsToInt32s takes a slice of strings and returns a slice of int32s

func StringsToInts

func StringsToInts(strings []string) ([]int, error)

StringsToInts takes a slice of strings and returns a slice of ints

func Sum

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

Sum gives the sum of a slice of ints or floats.

func SurroundingNewlineTrimmedLines

func SurroundingNewlineTrimmedLines(s string) []string

SurroundingNewlineTrimmedLines trimes leading and trailing newlines from the input, then splits on newlines.

func Transpose

func Transpose(input [][]int) [][]int

func TrimmedLines

func TrimmedLines(s string) []string

TrimmedLines takes a string, splits it into lines, and trims each line of starting and ending whitespace.

Types

type Number

type Number interface {
	constraints.Float | constraints.Integer
}

Number is a Float or Integer

type StringsAndInts

type StringsAndInts struct {
	Strings []string
	Ints    []int
	Input   string // the original input, when appropriate
}

StringsAndInts is a set of parsed strings and ints from a line of input.

func ParseByRegexps

func ParseByRegexps(inputs []string, regexps []*regexp.Regexp) ([]StringsAndInts, error)

ParseByRegexps applies the given list of regexps (in order) to each line of the input. When one matches, the groups in it are parsed into integers if possible, strings otherwise.

func ParseStringsAndInts

func ParseStringsAndInts(lines []string, fields int, stringFields []int, intFields []int) ([]StringsAndInts, error)

ParseStringsAndInts takes a slice of input lines, a slice of string field indexes, and a slice of int field indexes. It returns a slice of StringsAndInts structs, one per line.

Jump to

Keyboard shortcuts

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