common

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 3 Imported by: 24

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Real](n T) T

Abs returns the absolute value of the given Real number.

func AddSentinal added in v0.0.6

func AddSentinal(lines []string, c string) []string

func AddSentinal2 added in v0.0.6

func AddSentinal2[T any](lines [][]T, c T) [][]T

func ByteSlice2ToStringSlice added in v0.0.6

func ByteSlice2ToStringSlice(bs [][]byte) []string

func DigitToInt added in v0.0.6

func DigitToInt(b byte) int

func Fjoin

func Fjoin[T any](elems []T, sep string, str func(e T) string) string

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

func IsDigit(b byte) bool

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

func PadToLeft(s, p string, c int) string

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

func PadToRight(s, p string, c int) string

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 Padding

func Padding(p string, r int) string

Padding generates a padding string made up of |r| repeated copies of a string |p|.

func Panicf added in v0.0.6

func Panicf(fmtStr string, args ...any)

func SliceIndex added in v0.0.6

func SliceIndex[T Equatable](slice []T, target []T) int

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

func StringSliceToByteSlice2(strs []string) [][]byte

Types

type ConvenientLenable

type ConvenientLenable[T any] interface {
	~string | ~[]T
}

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.

Jump to

Keyboard shortcuts

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