utils

package module
v0.0.0-...-1d13650 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: AGPL-3.0 Imports: 17 Imported by: 0

README

utils

A set of small utils for #golang.

(c) Roger Sen Montero [email protected] 2024

Work in progress. APIs will change. Some functions will panic()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Numbers](x T) T

Abs returns the absolute value of x.

func AsFloat

func AsFloat(value int) float64

function int --> float64

func AsString

func AsString(value int) string

func Atoi

func Atoi(StrNum string, ResultIfFail int) int

Atoi converts StrNum to an intenger, returning ResultIfFails if it's not possible. It is equivalent to strconv.Atoi

func CurrentFunction

func CurrentFunction() string

returns the name of the current function

func DeepCopy

func DeepCopy(src, dest interface{}) (err error)

deepcopy src to dest

func ErrorIf

func ErrorIf(condition bool, err error) (noerror error)

return err if meets condition

func False

func False[T any](value ...T) bool

False always returs true False(4) = false False(true) = false

func GenerateFileName

func GenerateFileName(tail string) string

generate filename in RFC3339 format: 2024-03-07T09:39:22+01:00-world.json

GenerateFileName("-world.json") --> 2024-03-07T09:39:22+01:00-world.json

func Identity

func Identity[T any](item T) T

identity returns its argument

Identity(4) = 4
Identity(true) = true

func If

func If[T any](condition bool, value T) (Nil T)

return value if meets condition, otherwise nil

func IfElse

func IfElse[T any](condition bool, yes T, no T) T

ternary if operator

func IfFalse

func IfFalse[T any](condition bool, value T) (Nil T)

return value if doesn't meet condition, otherwise nil

func IsSlice

func IsSlice[T any](v T) bool

IsSlice returns true if v is a slice or Array

func Map

func Map[T, R any](function func(T) R, slice []T) (output []R)

Map function to every element of the slice returinng a new slice with the output

Map(abs, []int{-1, 2, -3, 4}) = []int{1, 2, 3, 4}

func None

func None[T any](typ_ T) (output T)

None returns the nil value of T None(4) = 0 None(true) = false

func ParentFunction

func ParentFunction() string

returns the name of the parent function

func PositiveOrZero

func PositiveOrZero[T Numbers](x T) T

PositiveOrZero returns the positive number or 0 if negative.

PositiveOrZero(x) = x
PositiveOrZero(-x) = 0

func Regex_CaptureAllAsStrings

func Regex_CaptureAllAsStrings(input string, regex string) (output []string)

func Regex_CaptureAsInt

func Regex_CaptureAsInt(input string, regex string) (output int)

func Regex_CaptureAsString

func Regex_CaptureAsString(input string, regex string) string

func Regex_Count_Matches

func Regex_Count_Matches(input string, regex string) int

func Regex_Match

func Regex_Match(input string, regex string) bool

func Slice2Map

func Slice2Map[T2 constraints.Ordered, T1 Indexable[T2]](slice []T1, filter []T2) (out map[T2]T1)

func True

func True[T any](value T) bool

True always returs true True(4) = true True(false) = true

func ValueAsFloat

func ValueAsFloat(input any) (output float64, err error)

1 --> 1.0 1.0 --> 1.0 "1.0" --> 1.0

Types

type Histogram

type Histogram[K comparable] struct {
	// contains filtered or unexported fields
}

Histogram calculates the frequency of value K treating any negative V as zero. So if we remove K when freq(K) == 0 we do not update its frequency.

func (Histogram[K]) Add

func (d Histogram[K]) Add(key K) (NewValue int)

func (Histogram[K]) Empty

func (d Histogram[K]) Empty() bool

func (Histogram[K]) Len

func (d Histogram[K]) Len() int

func (Histogram[K]) Remove

func (d Histogram[K]) Remove(key K) (NewValue int)

type HistogramPlus

type HistogramPlus[K comparable] struct {
	// contains filtered or unexported fields
}

HistogramPlus calculates the frequency of value K treating any negative V as negative. So if we remove K when freq(K) == 0 then freq(K) == -1

func (HistogramPlus[K]) Add

func (d HistogramPlus[K]) Add(key K) (NewValue int)

func (HistogramPlus[K]) IsZero

func (d HistogramPlus[K]) IsZero() bool

func (HistogramPlus[K]) Len

func (d HistogramPlus[K]) Len() int

func (HistogramPlus[K]) Remove

func (d HistogramPlus[K]) Remove(key K) (NewValue int)

type Indexable

type Indexable[T constraints.Ordered] interface {
	Key() (value T, name string)
}

type MultiString

type MultiString []string

func Table2MultiLine

func Table2MultiLine(table string) (output MultiString)

convert a list of lines into []string so we can randomly select a line

type Numbers

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳