Documentation
¶
Overview ¶
Package common contains a set of miscellaneous helper functions (generic, when applicable) that should maybe be part of Go's standard library.
Index ¶
- func Abs[T Real](n T) T
- func AddSentinal(lines []string, c string) []string
- func AddSentinal2[T any](lines [][]T, c T) [][]T
- func ByteSlice2ToStringSlice(bs [][]byte) []string
- func DigitToInt(b byte) int
- func Fjoin[T any](elems []T, sep string, str func(e T) string) string
- func Fmax[T any, R constraints.Ordered](f func(e T) R, a, b T, rest ...T) R
- func Fmin[T any, R constraints.Ordered](f func(e T) R, a, b T, rest ...T) R
- func FsliceMax[T any, R constraints.Ordered](slice []T, f func(e T) R) R
- func FsliceMin[T any, R constraints.Ordered](slice []T, f func(e T) R) R
- func IsDigit(b byte) bool
- func Longest[T any, L ConvenientLenable[T]](s []L) int
- func Max[T constraints.Ordered](a, b T, rest ...T) T
- func Min[T constraints.Ordered](a, b T, rest ...T) T
- func PadToLeft(s, p string, c int) string
- func PadToRight(s, p string, c int) string
- func Padding(p string, r int) string
- func Panicf(fmtStr string, args ...any)
- func SliceIndex[T Equatable](slice []T, target []T) int
- func SliceMax[T constraints.Ordered](slice []T) T
- func SliceMin[T constraints.Ordered](slice []T) T
- func SliceSum[T Real](slice []T) T
- func SplitSlice[T Equatable](slice []T, sep []T) [][]T
- func StringSliceToByteSlice2(strs []string) [][]byte
- type ConvenientLenable
- type Equatable
- type Lenable
- type Real
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSentinal ¶ added in v0.0.6
func AddSentinal2 ¶ added in v0.0.6
func AddSentinal2[T any](lines [][]T, c T) [][]T
func ByteSlice2ToStringSlice ¶ added in v0.0.6
func DigitToInt ¶ added in v0.0.6
func Fjoin ¶
Fjoin combines a slice of values into a single string separated by |sep|. This is the same as the built-in strings.Join() except that the caller needs to pass in a function to convert the input values into strings.
func Fmax ¶
func Fmax[T any, R constraints.Ordered](f func(e T) R, a, b T, rest ...T) R
Fmax returns the maximum Ordered value that results from applying the given function to each of the given values.
func Fmin ¶
func Fmin[T any, R constraints.Ordered](f func(e T) R, a, b T, rest ...T) R
Fmin returns the minimum Ordered value that results from applying the given function to each of the given values.
func FsliceMax ¶
func FsliceMax[T any, R constraints.Ordered](slice []T, f func(e T) R) R
FsliceMax returns the maximum Ordered value that results from applying the given function to each individual element of a slice.
func FsliceMin ¶
func FsliceMin[T any, R constraints.Ordered](slice []T, f func(e T) R) R
FsliceMin returns the minimum Ordered value that results from applying the given function to each individual element of a slice.
func Longest ¶
func Longest[T any, L ConvenientLenable[T]](s []L) int
Longest returns the longest ConvenientLenable out of a slice.
func Max ¶
func Max[T constraints.Ordered](a, b T, rest ...T) T
Max returns the maximum element of the given Ordered values.
func Min ¶
func Min[T constraints.Ordered](a, b T, rest ...T) T
Min returns the minimum element of the given Ordered values.
func PadToLeft ¶
PadToLeft takes in a string, |s|, a padding string |p|, and a target number of characters, |c|. If len(s) >= c, it returns s. Otherwise, it returns a string consisting of some amount of repititions of |p|, followed by |s|, such that the total length of the return value is as close as possible to |c|.
if (len(s) - r) % len(p) != 0, this won't be aligned. Usually best to stick with len(p) = 1
Also, if len(p) == 0, this will crash.
func PadToRight ¶
PadToRight takes in a string, |s|, a padding string |p|, and a target number of characters, |c|. If len(s) >= c, it returns s. Otherwise, it returns a string consisting of |s| followed by some amount of repititions of |p|, such that the total length of the return value is as close as possible to |c|.
if (len(s) - r) % len(p) != 0, this won't be aligned. Usually best to stick with len(p) = 1
Also, if len(p) == 0, this will crash.
func SliceIndex ¶ added in v0.0.6
func SliceMax ¶
func SliceMax[T constraints.Ordered](slice []T) T
SliceMax returns the maximum element of a slice of Ordered values.
func SliceMin ¶
func SliceMin[T constraints.Ordered](slice []T) T
SliceMin returns the minimum element of a slice of Ordered values.
func SliceSum ¶
func SliceSum[T Real](slice []T) T
SliceSum returns the total sum of a slice of Real numbers.
func SplitSlice ¶ added in v0.0.6
func SplitSlice[T Equatable](slice []T, sep []T) [][]T
func StringSliceToByteSlice2 ¶ added in v0.0.6
Types ¶
type ConvenientLenable ¶
ConvenientLenable is the set of non-map types than len() makes sense on.
type Equatable ¶ added in v0.0.6
type Equatable interface { constraints.Complex | constraints.Float | constraints.Integer | ~string | ~bool | ~byte }
type Lenable ¶
type Lenable[T any, C comparable] interface { ConvenientLenable[T] | ~map[C]T }
Lenable is the set of types than len() makes sense on. maps make this really annoying because they require a second type constraint which can't be automatically inferred if you want to use one of the non-map lenable types.
type Real ¶
type Real interface { constraints.Integer | constraints.Float }
Real is all non-complex number types.