g

package module
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: MIT Imports: 6 Imported by: 0

README

G package

commonly used functions in go.package g.

Usage

func main() {
  g.Contains([]int{1, 2, 3}, 1)
  g.Reverse([]int{1, 2, 3})
}

All functions

// import "github.com/sleagon/g"


FUNCTIONS

func Contains[T comparable](values []T, v T) bool
    Contains check v is contained by values.

func Filter[T any](values []T, f func(T, int) bool) []T
    Filter is a filter operator, like java's filter.

func Flat2[T any](values [][]T) []T
    Flat2 flatten 2-dimensional array to 1-dimensional array.

func Flat3[T any](values [][][]T) []T
    Flat3 flatten 3-dimensional array to 1-dimensional array.

func GroupBy[T any, K comparable](values []T, f func(T, int) K) map[K][]T
    GroupBy is a group by operator, like guava's group by.

func IndexOf[T comparable](values []T, v T) int
    IndexOf find the index of v in values, return -1 if element not found.

func IndexOfBy[T comparable](values []T, f func(T, int) bool) int
    IndexOfBy find the index of v in values, return -1 if element not found.

func Keys[K comparable, V any](values map[K]V) []K
    Keys returns the keys of the map, order is not guaranteed.

func LastIndexOf[T comparable](values []T, v T) int
    LastIndexOf find the last index of v in values, return -1 if element not
    found.

func LastIndexOfBy[T comparable](values []T, f func(T, int) bool) int
    LastIndexOfBy find the last index of v in values, return -1 if element not
    found.

func Map[F any, T any](values []F, f func(F, int) T) []T
    Map is a map operator, like js/java's map.

func MapEntries[K comparable, V any, T any](values map[K]V, f func(K, V) T) []T
    MapEntries returns the f(entries) of the map, order is not guaranteed.

func MapKeys[K comparable, V any, T any](values map[K]V, f func(K) T) []T
    MapKeys returns the f(key)s of the map, order is not guaranteed.

func MapValues[K comparable, V any, T any](values map[K]V, f func(V) T) []T
    MapValues returns the f(value)s of the map, order is not guaranteed.

func Max[T Numberic](values []T) T
    Max returns the maximum value in the given slice.

func MaxBy[T any](values []T, less func(T, T) bool) T
    MaxBy returns the maximum value in the given slice, using the provided less
    function to compare values.

func MaxKey[K Numberic, V any](values map[K]V) K
    MaxKey returns the maximum key of the map.

func MaxKeyBy[K comparable, V any](values map[K]V, less func(K, K) bool) K
    MaxKeyBy returns the maximum key of the map based on less method.

func MaxValue[K comparable, V Numberic](values map[K]V) V
    MaxValue returns the maximum value of the map.

func MaxValueBy[K comparable, V any](values map[K]V, less func(V, V) bool) V
    MaxValueBy returns the maximum value of the map based on less method.

func Min[T Numberic](values []T) T
    Min returns the minimum value in the given slice.

func MinBy[T any](values []T, less func(T, T) bool) T
    MinBy returns the minimum value in the given slice, using the provided less
    function to compare values.

func MinKey[K Numberic, V any](values map[K]V) K
    MinKey returns the minimum key of the map.

func MinKeyBy[K comparable, V any](values map[K]V, less func(K, K) bool) K
    MinKeyBy returns the minimum key of the map based on less method.

func MinValue[K comparable, V Numberic](values map[K]V) V
    MinValue returns the minimum value of the map.

func MinValueBy[K comparable, V any](values map[K]V, less func(V, V) bool) V
    MinValueBy returns the minimum value of the map based on less method.

func Must[T any](v T, err error) T
    Must assert a condition, and if it fails, panic.

func Must0(err error)
    Must0 assert err is nil

func Must1[T any](v T, err error) T
    MustN assert n conditions, and if it fails, panic.

func Must2[T1 any, T2 any](v1 T1, v2 T2, err error) (T1, T2)
    MustN assert n conditions, and if it fails, panic.

func Must3[T1 any, T2 any, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3)
    MustN assert n conditions, and if it fails, panic.

func Must4[T1 any, T2 any, T3 any, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4)
    MustN assert n conditions, and if it fails, panic.

func Partition[T any](values []T, size int) [][]T
    Partition is a partition operator, like java's partition.

func PutAll[K comparable, V any](target map[K]V, elements map[K]V) map[K]V
    PutAll add all elements to exist map, if the key is already exists, the
    value will be overridden.

func Reduce[F any, T any](values []F, f func(T, F, int) T) T
    Reduce is a reduce operator, like js's reduce.

func Reverse[T any](values []T) []T
    Reverse reverse the order of elements in values.

func ReverseS(s string) string
    ReverseS returns a string with the reverse order of the given string.

func Slice2Map[K comparable, V any](values []V, f func(V, int) K) map[K]V
    Slice2Map transfer slice to a map base on given func.

func Ternary[T any](cond bool, v1 T, v2 T) T
    Ternary is a ternary operator, like C's ?:.

func TernaryF[T any](cond bool, f1 func() T, f2 func() T) T
    If is a if-else operator, like C's if-else.

func Uniq[T comparable](values []T) []T
    Uniq remove duplicated elements from values

func ValueOf[T any](ptr *T) T
    ValueOf returns the value of the given pointer.

func Values[K comparable, V any](values map[K]V) []V
    Values returns the values of the map, order is not guaranteed.


TYPES

type Numberic interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}
    Numberic is a numeric type, which can use ><= operators.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySlice added in v0.3.1

func AnySlice[T any](values []T) []any

AnySlice convert []T to []interface{}/[]any.

func AssertSlice added in v0.3.1

func AssertSlice[T any](values []any) []T

AssertSlice assert interface list to target type, panic if elements of values are not T.

func Contains

func Contains[T comparable](values []T, v T) bool

Contains check v is contained by values.

func CopyMap added in v0.3.5

func CopyMap[K comparable, V any](values map[K]V) map[K]V

CopyMap returns a copy of the map.

func CopySlice added in v0.3.5

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

CopySlice copy slice.

func Filter

func Filter[T any](values []T, f func(T, int) bool) []T

Filter is a filter operator, like java's filter.

func Flat2

func Flat2[T any](values [][]T) []T

Flat2 flatten 2-dimensional array to 1-dimensional array.

func Flat3

func Flat3[T any](values [][][]T) []T

Flat3 flatten 3-dimensional array to 1-dimensional array.

func Go added in v0.3.6

func Go[S any, T any](funcs ...Go1Func[S, T]) func(context.Context, []S) ([]T, error)

func Go0 added in v0.3.6

func Go0[T any](funcs ...Go0Func[T]) func(context.Context) ([]T, error)

func Go1 added in v0.3.6

func Go1[S any, T any](funcs ...Go1Func[S, T]) func(context.Context, []S) ([]T, error)

func Go2 added in v0.3.6

func Go2[S1 any, S2 any, T any](funcs ...Go2Func[S1, S2, T]) func(context.Context, []S1, []S2) ([]T, error)

func Go3 added in v0.3.6

func Go3[S1 any, S2 any, S3 any, T any](funcs ...Go3Func[S1, S2, S3, T]) func(context.Context, []S1, []S2, []S3) ([]T, error)

func GroupBy

func GroupBy[T any, K comparable](values []T, f func(T, int) K) map[K][]T

GroupBy is a group by operator, like guava's group by.

func IndexOf

func IndexOf[T comparable](values []T, v T) int

IndexOf find the index of v in values, return -1 if element not found.

func IndexOfBy

func IndexOfBy[T comparable](values []T, f func(T, int) bool) int

IndexOfBy find the index of v in values, return -1 if element not found.

func Keys

func Keys[K comparable, V any](values map[K]V) []K

Keys returns the keys of the map, order is not guaranteed.

func LastIndexOf

func LastIndexOf[T comparable](values []T, v T) int

LastIndexOf find the last index of v in values, return -1 if element not found.

func LastIndexOfBy

func LastIndexOfBy[T comparable](values []T, f func(T, int) bool) int

LastIndexOfBy find the last index of v in values, return -1 if element not found.

func Map

func Map[F any, T any](values []F, f func(F, int) T) []T

Map is a map operator, like js/java's map.

func MapEntries

func MapEntries[K comparable, V any, T any](values map[K]V, f func(K, V) T) []T

MapEntries returns the f(entries) of the map, order is not guaranteed.

func MapKeys

func MapKeys[K comparable, V any, T any](values map[K]V, f func(K) T) []T

MapKeys returns the f(key)s of the map, order is not guaranteed.

func MapValues

func MapValues[K comparable, V any, T any](values map[K]V, f func(V) T) []T

MapValues returns the f(value)s of the map, order is not guaranteed.

func Max

func Max[T Numberic](values []T) T

Max returns the maximum value in the given slice.

func MaxBy

func MaxBy[T any](values []T, less func(T, T) bool) T

MaxBy returns the maximum value in the given slice, using the provided less function to compare values.

func MaxKey

func MaxKey[K Numberic, V any](values map[K]V) K

MaxKey returns the maximum key of the map.

func MaxKeyBy

func MaxKeyBy[K comparable, V any](values map[K]V, less func(K, K) bool) K

MaxKeyBy returns the maximum key of the map based on less method.

func MaxValue

func MaxValue[K comparable, V Numberic](values map[K]V) V

MaxValue returns the maximum value of the map.

func MaxValueBy

func MaxValueBy[K comparable, V any](values map[K]V, less func(V, V) bool) V

MaxValueBy returns the maximum value of the map based on less method.

func Min

func Min[T Numberic](values []T) T

Min returns the minimum value in the given slice.

func MinBy

func MinBy[T any](values []T, less func(T, T) bool) T

MinBy returns the minimum value in the given slice, using the provided less function to compare values.

func MinKey

func MinKey[K Numberic, V any](values map[K]V) K

MinKey returns the minimum key of the map.

func MinKeyBy

func MinKeyBy[K comparable, V any](values map[K]V, less func(K, K) bool) K

MinKeyBy returns the minimum key of the map based on less method.

func MinValue

func MinValue[K comparable, V Numberic](values map[K]V) V

MinValue returns the minimum value of the map.

func MinValueBy

func MinValueBy[K comparable, V any](values map[K]V, less func(V, V) bool) V

MinValueBy returns the minimum value of the map based on less method.

func Must

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

Must assert a condition, and if it fails, panic.

func Must0

func Must0(err error)

Must0 assert err is nil

func Must1

func Must1[T any](v T, err error) T

MustN assert n conditions, and if it fails, panic.

func Must2

func Must2[T1 any, T2 any](v1 T1, v2 T2, err error) (T1, T2)

MustN assert n conditions, and if it fails, panic.

func Must3

func Must3[T1 any, T2 any, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3)

MustN assert n conditions, and if it fails, panic.

func Must4

func Must4[T1 any, T2 any, T3 any, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4)

MustN assert n conditions, and if it fails, panic.

func Partition

func Partition[T any](values []T, size int) [][]T

Partition is a partition operator, like java's partition.

func Ptr

func Ptr[T any](v T) *T

Ptr returns the pointer of the given value, Ptr will panic if v is nil.

func PutAll

func PutAll[K comparable, V any](target map[K]V, elements map[K]V) map[K]V

PutAll add all elements to exist map, if the key is already exists, the value will be overridden.

func Reduce

func Reduce[F any, T any](values []F, f func(T, F, int) T) T

Reduce is a reduce operator, like js's reduce.

func Repeat added in v0.3.9

func Repeat[T any](v T, n int) []T

func Reverse

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

Reverse reverse the order of elements in values.

func ReverseS

func ReverseS(s string) string

ReverseS returns a string with the reverse order of the given string.

func Slice2Map

func Slice2Map[K comparable, V any](values []V, f func(V, int) K) map[K]V

Slice2Map transfer slice to a map base on given func.

func Ternary

func Ternary[T any](cond bool, v1 T, v2 T) T

Ternary is a ternary operator, like C's ?:.

func TernaryF

func TernaryF[T any](cond bool, f1 func() T, f2 func() T) T

If is a if-else operator, like C's if-else.

func Uniq

func Uniq[T comparable](values []T) []T

Uniq remove duplicated elements from values

func UniqBy added in v0.3.1

func UniqBy[V any, T comparable](values []V, f func(V) T) []V

UniqBy remove duplicated elements from values, based on given func.

func ValueOf

func ValueOf[T any](ptr *T) T

ValueOf returns the value of the given pointer.

func Values

func Values[K comparable, V any](values map[K]V) []V

Values returns the values of the map, order is not guaranteed.

Types

type Go0Func added in v0.3.6

type Go0Func[T any] func(context.Context) (T, error)

type Go1Func added in v0.3.6

type Go1Func[S any, T any] func(context.Context, S) (T, error)

type Go2Func added in v0.3.6

type Go2Func[S1 any, S2 any, T any] func(context.Context, S1, S2) (T, error)

type Go3Func added in v0.3.6

type Go3Func[S1 any, S2 any, S3 any, T any] func(context.Context, S1, S2, S3) (T, error)

type JMap added in v0.3.8

type JMap map[string]any

func (JMap) MarshalJSON added in v0.3.8

func (j JMap) MarshalJSON() ([]byte, error)

func (*JMap) UnmarshalJSON added in v0.3.8

func (j *JMap) UnmarshalJSON(bts []byte) error

type Numberic

type Numberic interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}

Numberic is a numeric type, which can use ><= operators.

type SyncMap added in v0.3.2

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSyncMap added in v0.3.2

func NewSyncMap[K comparable, V any]() SyncMap[K, V]

func (*SyncMap[K, V]) Delete added in v0.3.2

func (m *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) Load added in v0.3.2

func (m *SyncMap[K, V]) Load(key K) (V, bool)

func (*SyncMap[K, V]) LoadAndDelete added in v0.3.2

func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool)

func (*SyncMap[K, V]) LoadOrStore added in v0.3.2

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool)

func (*SyncMap[K, V]) Range added in v0.3.2

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

func (*SyncMap[K, V]) Snapshot added in v0.3.2

func (m *SyncMap[K, V]) Snapshot() map[K]V

Snapshot returns a copy of the map based on Range method.

func (*SyncMap[K, V]) Store added in v0.3.2

func (m *SyncMap[K, V]) Store(key K, value V)

Jump to

Keyboard shortcuts

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