funcs

package
v0.0.0-...-b6a56d8 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Between

func Between[T constraints.Ordered](value, lowerBound, upperBound T) bool

Between returns if value is in [lowerBound, upperBound].

func Clone

func Clone[M ~map[K]V, K comparable, V any](m M) M

Clone returns a copy of m.

func Combine

func Combine[T, R, S any](first func(T) R, second func(R) S) func(T) S

Combine two functions as one.

func Contains

func Contains[T comparable, S ~[]T](slice S, elem T) bool

Contains reports whether elem is present in slice.

func ContainsAll

func ContainsAll[T comparable, S ~[]T](slice S, elems []T) bool

ContainsAll reports whether all elems are present in slice.

func ContainsAllKeys

func ContainsAllKeys[M ~map[K]V, K comparable, V any](m M, keys []K) bool

ContainsAllKeys returns whether all keys are present in map.

func ContainsAny

func ContainsAny[T comparable, S ~[]T](slice S, elems []T) bool

ContainsAny reports whether anyone is present in slice.

func ContainsAnyKey

func ContainsAnyKey[M ~map[K]V, K comparable, V any](m M, keys []K) bool

ContainsAnyKey returns whether any key is present in map.

func ContainsKey

func ContainsKey[M ~map[K]V, K comparable, V any](m M, key K) bool

ContainsKey returns whether key is present in map.

func CopyFunc

func CopyFunc[T any](val T) func() T

CopyFunc transfer value to a function.

func Equal

func Equal[T comparable](a, b T) bool

Equal returns if a equals to b

func Fill

func Fill[T any, S ~[]T](slice S, initial T) []T

Fill initial to each element of slice.

func FillBy

func FillBy[T any, S ~[]T](slice S, f func(index int) T) []T

FillBy f to each element of slice.

func ForEach

func ForEach[T any](slice []T, f func(T) bool) bool

ForEach apply f to each element of slice until f returns false.

func ForEachKV

func ForEachKV[M ~map[K]V, K comparable, V any](m M, f func(K, V) bool) bool

ForEachKV apply f to each KV pair of map until f returns false.

func ForEachPtr

func ForEachPtr[T any](slice []T, f func(*T) bool) bool

ForEachPtr apply f to each element of slice until f returns false.

func ForRange

func ForRange[T constraints.Integer | constraints.Float](start, end, step T, f func(T) bool) bool

ForRange apply f to each number in a sequence construct with step in range [start, end).

func FromPtr

func FromPtr[T any](x *T) T

FromPtr returns the pointer value or empty.

func FromPtrOr

func FromPtrOr[T any](x *T, fallback T) T

FromPtrOr returns the pointer value or the fallback value.

func IndexFunc

func IndexFunc[T any, S ~[]T](slice S) func(int) T

IndexFunc convert slice to a function which input is index and output is slice[index].

func IsZero

func IsZero[T comparable](value T) bool

IsZero returns value is zero.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns all keys of map.

func LargerThan

func LargerThan[T constraints.Ordered](a, b T) bool

LargerThan returns if a is larger than b

func LessThan

func LessThan[T constraints.Ordered](a, b T) bool

LessThan returns if a is less than b

func Map

func Map[T any, R any, S ~[]T](slice S, iteratee func(item T) R) []R

Map manipulates a slice and transforms it to a slice of another type.

func MapPtr

func MapPtr[T any, R any, S ~[]T](slice S, iteratee func(item *T) R) []R

MapPtr manipulates a slice and transforms each element's pointer of it to a slice of another type.

func Max

func Max[T constraints.Ordered](origin T, slice ...T) T

Max returns the maximum value between origin and each element of slice

func Max2

func Max2[T constraints.Ordered](a, b T) T

Max2 returns the maximum value between a and b

func Merge

func Merge[M ~map[K]V, K comparable, V any](maps ...M) M

Merge merges multiple maps from left to right.

func Min

func Min[T constraints.Ordered](origin T, slice ...T) T

Min returns the minimum value between origin and each element of slice

func Min2

func Min2[T constraints.Ordered](a, b T) T

Min2 returns the minimum value between a and b

func Must

func Must[T any](value T, err error) T

Must is a helper function to panic if an error occurs.

func MustTrue

func MustTrue[T any](value T, ok bool) T

MustTrue is a helper function to panic if ok is false.

func Pack

func Pack[T any](elems ...T) []T

Pack returns a slice built by elems.

func Reduce

func Reduce[T any, R any](slice []T, initial R, accumulator func(agg R, item T) R) R

Reduce reduces slice to an accumulated result of running each element in slice by accumulator.

func ReflectIF

func ReflectIF[T any](value any, cond bool) (T, bool)

ReflectIF returns value after reflect or zero if reflect failed.

func ReflectOR

func ReflectOR[T any](value any, fallback T) T

ReflectOR returns value after reflect or the fallback value if reflect failed.

func Repeat

func Repeat[T any](len int, initial T) []T

Repeat returns a slice that each element equals to initial

func RepeatBy

func RepeatBy[T any](len int, f func(index int) T) []T

RepeatBy returns a slice that each element generated by f.

func Reverse

func Reverse[T any, S ~[]T](slice S)

Reverse the element order of slice.

func SliceToMap

func SliceToMap[K comparable, V any, S ~[]K](slice S, f func(K) V) map[K]V

SliceToMap convert slice to map.

func Sort

func Sort[T constraints.Ordered, S ~[]T](slice S)

Sort sorts a slice of any ordered type in ascending order.

func SortBy

func SortBy[T any, S ~[]T](slice S, less func(a, b T) bool)

SortBy sorts the slice by less functions.

func SortedKeys

func SortedKeys[M ~map[K]V, K constraints.Ordered, V any](m M) []K

SortedKeys returns all keys of map in order.

func Sum

func Sum[T constraints.Ordered](origin T, slice ...T) T

Sum returns the summation between origin and each element of slice

func Sum2

func Sum2[T constraints.Ordered](a, b T) T

Sum2 returns the summation between a and b

func Swap

func Swap[T any, R any](first T, second R) (R, T)

Swap two args.

func Ternary

func Ternary[T any](cond bool, t T, f T) T

Ternary is a 1 line if/else statement.

func ToPtr

func ToPtr[T any](x T) *T

ToPtr returns a pointer copy of value.

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase transfer str to snake case. Such as MyStr => my_str

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns all values of map.

func WithArg

func WithArg[T, R any](f func() R) func(T) R

WithArg injects one arg to func f.

func WithResult

func WithResult[T, R any](f func(T), r R) func(T) R

WithResult combines func f and result r.

func Zero

func Zero[T any]() T

Zero returns an zero value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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