gsl

package module
v0.0.0-...-e5e7391 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: BSD-3-Clause Imports: 8 Imported by: 5

README

gsl: Go Soylib

gsl provides frequently used functionalities that I use across my projects.

It is my personal learning project.

Since Go is focused on simplicity, some battery-included kind of built-ins are not included in Go (and that's a good thing). This led programmers to manually implement these basic solutions, and this led to a lot of duplicate code, oversighted bugs, etc.

This is where gsl comes in - to standardize and store frequently used generic business logic for soy and non-soy devs alike.

Since 2023, Go 1.21 was released with many "battery-included" standard packages, such as "slices", that does many of the things gsl does, so you might want to explore those packages first before gsl.

Go also has released many official extra packages.

  • gsl - basic utilities for everyday use

  • soyutils - utilities for soy devs

  • http - providing wrapper for Go's http package

  • data - basic data structure library. Highly unstable, because this is where I learn to code data structures

  • concurrent - simple stupid concurrency helper functions

  • sqlquery - interface-based SQL query builder to complement squirrel on features such as INSERT ALL or upserts

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConvertible = errors.New("types are not convertible")
View Source
var ErrRetry = errors.New("error when retrying")

Functions

func Avg

func Avg[T constraints.Integer | constraints.Float](items ...T) T

func CloseChar

func CloseChar(openChar rune) (rune, error)

func CollectPointers

func CollectPointers[T any](arr []T) []*T

CollectPointers iterates over |arr| and returns a new slice containing all references to the elements of |arr|.

func CollectPointersIf

func CollectPointersIf[T any](arr []T, filterFunc func(elem T) bool) []*T

CollectPointersIf iterates over |arr| and calling filterFunc on the element |elem|. If filterFunc returns true, |elem| is added to the return slice.

func CompareInterfaceValues

func CompareInterfaceValues[T comparable](a, b interface{}) (bool, error)

CompareInterfaceValues will compares |a| and |b| as type T. It returns true if |a| and |b| are of type T and are equal in value, or if |a| and |b| can be converted to T and are equal in value. If |a| and |b| are not equal but can be converted to T, then it returns false. CompareInterfaceValues *only* returns non-nil error if any of the values cannot be converted into T.

func Contains

func Contains[T comparable](arr []T, item T) bool

Contains iterates over |arr| and returns true if an element in |arr| is equal to |item|.

func CopySlice

func CopySlice[T any](arr []T) []T

CopySlice return a copy of |arr|.

func DefaultValue

func DefaultValue[T interface{}]() T

DefaultValue is just an alias to ZeroedValue

func DerefValues

func DerefValues[T any](arr []*T) []T

DerefValues iterates over |arr| and return a new slice containing dereferenced values of the elements in |arr|. If the pointer at `arr[i]` is nil, the returned slice will have default value of type `T` at index `i`.

func DerefValuesIf

func DerefValuesIf[T any](arr []*T, filterFunc func(elem T) bool) []T

DerefValuesIf iterates over |arr| and calls |filterFunc| on the element |elem|. If filterFunc returns true, |elem|'s dereferenced value is added to the return slice. Unlike DerefValues but similar to CollectPointersIf, if |elem| is nil, its value will not be added to the return slice

func FilterSlice

func FilterSlice[T any](arr []T, filterFunc func(elem T) bool) []T

FilterSlice iterates over |arr| and calls |filterFunc| on each of the element |elem|. If filterFunc returns true, |elem| is added to the return slice.

func GroupConsecutive

func GroupConsecutive[N GoNumber](s []N) [][2]N

GroupConsecutive *sorts* slice |s| in-place, and calls GroupConsecutiveSorted on |s|.

func GroupConsecutiveSorted

func GroupConsecutiveSorted[N GoNumber](s []N) [][2]N

GroupConsecutiveSorted groups input numbers in slice []N |s| that are consecutive to each other (i.e. difference of 1) into a slice of [2]N. The result type [2]N represents the start and end of a consecutive range, that is, result[0] is "from" while result[1] is "to" of such ranges.

If a breakpoint (non-consecutive element) is found, GroupConsecutive creates a new result. e.g. [1, 2, 3, 5, 6, 8, 9, 10] will be mapped to [{1, 3}, {5, 6}, {8, 10}].

If |s| has length of 0, it returns [][2]N{ {0, 0} }. If |s| has length of 1 and is []N{n}, it returns [][2]N{ {n, n} }.

func InterfaceTo

func InterfaceTo[T any](v interface{}) (T, error)

InterfaceTo converts v from interface{} to T. It returns a zeroed T and an error if not convertible.

func IsCloseChar

func IsCloseChar(closeChar rune) bool

func IsOpenChar

func IsOpenChar(openChar rune) bool

func IsWellClosed

func IsWellClosed(s string) error

IsWellClosed validates if all surrounds are properly closed

Examples (see unit test)

1. ([bar(foo)]) -> ok

2. [()[{}]] -> ok

3. [((] -> error

4. [) -> error

5. [ -> error

Notes: Only openning/closing characters are concerned.

func Map

func Map[T any, U any](
	arr []T,
	mapFunc func(elem T) (U, bool),
) []U

Map maps |arr| to a new slice using |mapFunc|. |mapFunc| returns a tuple of generic type U and a boolean. If |mapFunc|'s 2nd return value is true, its 1st return value (of type U) will be appended to the result slice. Note: `T` and `U` can be of the same type.

func Max

func Max[T constraints.Ordered](items ...T) T

func Min

func Min[T constraints.Ordered](items ...T) T

func OpenChar

func OpenChar(closeChar rune) (rune, error)

func Retry

func Retry(action string, f func() error, opts ...RetryOption) error

Retry wraps retry with action string.

func RetryWithReturn

func RetryWithReturn[T any](
	action string,
	f func() (T, error),
	opts ...RetryOption,
) (
	T,
	error,
)

RetryWithReturn wraps |f| in a `func() error` and captures the T value in that function, and returns T returned by |f|.

func Reverse

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

Reverse returns a new slice, with elements being those of |arr| after being reversed.

func ReverseInPlace

func ReverseInPlace[T any](arr []T)

ReverseInPlace reverses |arr| in-place.

func SliceFromMapKeys

func SliceFromMapKeys[K comparable, V any](m map[K]V) []K

SliceFromMapKeys collects map[K]V |m| into []K. If |m| is mil, SliceFromMapKeys returns nil.

func SliceFromMapKeysIf

func SliceFromMapKeysIf[K comparable, V any](m map[K]V, f func(K, V) bool) []K

SliceFromMapKeysIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the key in |m| gets appended to the return slice. If either |m| or |f| is nil, SliceFromMapKeysIf returns nil.

func SliceFromMapValues

func SliceFromMapValues[K comparable, V any](m map[K]V) []V

SliceFromMapValues collects map[K]V |m| into []V. If |m| is mil, SliceFromMapValues returns nil

func SliceFromMapValuesIf

func SliceFromMapValuesIf[K comparable, V any](m map[K]V, f func(K, V) bool) []V

SliceFromMapValuesIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the value in |m| gets appended to the return slice. If either |m| or |f| is nil, SliceFromMapValuesIf returns nil.

func SlicesFromMap

func SlicesFromMap[K comparable, V any](m map[K]V) ([]K, []V)

SlicesFromMap collects map[K]V |m| into ([]K, []V). If |m| is nil, SlicesFromMap returns nil.

func SlicesFromMapIf

func SlicesFromMapIf[K comparable, V any](m map[K]V, f func(K, V) bool) ([]K, []V)

SlicesFromMapIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the key and value in |m| gets appended to both the return slices. If either |m| or |f| is nil, SlicesFromMapIf returns nil, nil.

func StringerToLowerString

func StringerToLowerString(s stringer) string

StringerToLowerString calls String() on s and returns the result of calling `strings.ToLower` on that string.

func StringerToUpperString

func StringerToUpperString(s stringer) string

StringerToUpperString calls String() on s and returns the result of calling `strings.ToUpper` on that string.

func Sum

func Sum[T constraints.Integer | constraints.Float](items ...T) T

func ToLower

func ToLower[T ~string](s T) T

func ToUpper

func ToUpper[T ~string](s T) T

func ZeroedValue

func ZeroedValue[T any]() T

ZeroedValue returns default, zeroed value of type T.

Types

type GoNumber

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

type RetryOption

type RetryOption func(*retryConfig)

RetryOption is a function that takes in (and modifies) *Config

func Attempts

func Attempts(attempts int) RetryOption

Attempts set retry attempts

func Delay

func Delay(delay time.Duration) RetryOption

Delay sets retry delay

func LastErrorOnly

func LastErrorOnly(lastErrOnly bool) RetryOption

LastErrorOnly sets retry option to only return the last error

func StopOnError

func StopOnError(err error) RetryOption

StopOnError sets a specific error, which, if seen when retrying, breaks the retry loop. If |err| is found during retry, the loop breaks, and it is treated just like any other error, i.e. the retry still fails.

Jump to

Keyboard shortcuts

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