util

package
v0.0.0-...-5bd4ecb Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Integer | constraints.Float](n T) T

Abs returns an absolute value of the given integer or floating-point number.

func AllDirection

func AllDirection(y, x, rows, cols int) [][2]int

AllDirection is similar to CardinalDirection except this returns coordinates in all the directions from a given point, excluding the given point.

func CardinalDirection

func CardinalDirection(y, x, rows, cols int) [][]int

CardinalDirection returns a list of coordinates in the four cardinal directions from the given point (x, y).

The list contains only the coordinates which are within the matrix bounds as given by the rows and cols argument. The four cardinal directions are: North, South, East and West.

func Digits

func Digits(n int) <-chan int

Digits is used to iterate over each digit of the given number from left to right. This returns a channel from which you can only receive an integer one at a time and can be used in various ways like so:

for d := range Digits(123) {
  // do something with d
}

ch := Digits(123)
// ... other code
fmt.Println(<-ch)
// ... other code
fmt.Println(<-ch)

func MatrixCopy

func MatrixCopy(dest, src [][]int)

MatrixCopy is used to copy integer slice elements from source slice to a destination slice. Internally, this uses the built-in copy function to copy individual slice elements.

func Max

func Max[T constraints.Integer | constraints.Float](x, y T) T

Max returns the larger of x or y.

func Min

func Min[T constraints.Integer | constraints.Float](x, y T) T

Min returns the smaller of x or y.

func MinMax

func MinMax(sl []int) (int, int)

MinMax returns the minimum and maximum value in the given slice of integers. This will panic if the slice is empty.

func Mod

func Mod[T constraints.Integer](a, b T) T

Mod returns a % b, specifically the least positive remainder. This is different than the builtin % operator which returns the least negative remainder. This should only be used if either a or b is negative. Mod behaves the same as the builtin % operator when both a and b are positive.

func MustAtoi

func MustAtoi(s string) int

MustAtoi is like strconv.Atoi but panics if the conversion fails.

func MustBtoi

func MustBtoi(s string) int

MustBtoi is equivalent to util.MustParseInt(s, 2, 0), converted to type int.

func MustParseInt

func MustParseInt(s string, base int, bitSize int) int64

MustParseInt is like strconv.ParseInt but panics if the parsing fails.

func ReadLines

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

ReadLines is used to read the content of the file at a given path into a string slice where each element corresponds to a single line.

func ReadLinesAsInt

func ReadLinesAsInt(path string) ([]int, error)

ReadLinesAsInt is similar to ReadLines, except this will convert each line into an integer and return an int slice instead.

func ReadSections

func ReadSections(path string) ([][]string, error)

ReadSections is used to read the content of the file at a given path by sections. A section is defined as being separated by two newlines and each section is then converted into string slice where each element corresponds to a single line of that section.

func Reverse

func Reverse[T any](sl []T)

Reverse reverses the order of elements in the given slice in place.

func Signum

func Signum[T constraints.Integer | constraints.Float](n T) T

Returns a number representing sign of n.

  • 0 if the number is zero
  • 1 if the number is positive
  • -1 if the number is negative

func SortString

func SortString(s string) string

SortString is used to sort the individual characters in the given string.

func Sum

Sum is used to add all the integers in a given array.

func SumN

func SumN[T constraints.Integer](n T) T

SumN is used to sum 1 to n integers.

Types

This section is empty.

Jump to

Keyboard shortcuts

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