Documentation ¶
Index ¶
- func Contains[T comparable](sl []T, val T) bool
- func ContainsFunc[T any](sl []T, match matchFunc[T]) bool
- func Delete[T any](dst []T, idx int) (T, []T, error)
- func DeleteAfter[T any](dst []T, idx int) ([]T, error)
- func DeleteRange[T any](dst []T, r int, idx int) ([]T, error)
- func DeleteVal[T constraints.Ordered](dst []T, val T) ([]T, error)
- func ErrElemNotFound() error
- func ErrIdxOutOfRange(length int, idx int) error
- func Filter[Src any, Dst any](src []Src, filter func(src Src) (dst Dst, filter bool)) []Dst
- func Find[T comparable](sl []T, val T) (idx int, isFound bool)
- func FindAll[T comparable](sl []T, val T) (idx []int, isFound bool)
- func FindAllFunc[T any](sl []T, match matchFunc[T]) (idx []int, isFound bool)
- func FindFunc[T any](slice []T, match matchFunc[T]) (idx int, isFound bool)
- func FindLast[T comparable](sl []T, val T) (idx int, isFound bool)
- func FindLastFunc[T any](slice []T, match matchFunc[T]) (idx int, isFound bool)
- func Generator[T any](sl []T) <-chan T
- func Insert[T any](dst []T, value T, idx int) ([]T, error)
- func InsertSlice[T any](dst []T, src []T, idx int) ([]T, error)
- func Map[Src any, Dst any](src []Src, mapper func(src Src) Dst) []Dst
- func Max[T generickit.RealNumber](sl []T) T
- func Min[T generickit.RealNumber](sl []T) T
- func Reduce[Src any, Dst Number](src []Src, reducer func(src Src) Dst) Dst
- func Reverse[T any](s []T) []T
- func Shrink[T any](src []T) []T
- func Sum[T generickit.Number](sl []T) T
- type Number
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](sl []T, val T) bool
Example ¶
fmt.Println(Contains[int]([]int{1, 2, 3, 4, 5}, 5))
Output: true
func ContainsFunc ¶
func DeleteAfter ¶
Example ¶
res, _ := DeleteAfter[int]([]int{1, 2, 3, 4, 5}, 2) fmt.Println(res)
Output: [1 2]
func DeleteRange ¶
Example ¶
res, _ := DeleteRange[int]([]int{1, 2, 3, 4, 5}, 3, 1) fmt.Println(res)
Output: [1 5]
func DeleteVal ¶
func DeleteVal[T constraints.Ordered](dst []T, val T) ([]T, error)
Example ¶
res, _ := DeleteVal[int]([]int{1, 2, 3, 4, 5}, 2) fmt.Println(res)
Output: [1 3 4 5]
func ErrElemNotFound ¶
func ErrElemNotFound() error
func ErrIdxOutOfRange ¶
func Filter ¶
Example ¶
after := Filter[int, string]([]int{1, 2, 3, 4, 5}, func(src int) (string, bool) { return strconv.Itoa(src), src >= 3 }) fmt.Println(after)
Output: [3 4 5]
func Find ¶
func Find[T comparable](sl []T, val T) (idx int, isFound bool)
Example ¶
idx, isFound := Find[int]([]int{1, 2, 3, 4, 5, 5}, 3) fmt.Println(idx) fmt.Println(isFound)
Output: 2 true
func FindAll ¶
func FindAll[T comparable](sl []T, val T) (idx []int, isFound bool)
Example ¶
idxSlice, isFound := FindAll[int]([]int{1, 2, 3, 4, 5, 5}, 5) fmt.Println(idxSlice) fmt.Println(isFound)
Output: [4 5] true
func FindAllFunc ¶
FindAllFunc accepts a anonymous func to match
func FindLast ¶
func FindLast[T comparable](sl []T, val T) (idx int, isFound bool)
Example ¶
idx, isFound := Find[int]([]int{1, 2, 3, 4, 5, 5}, 5) fmt.Println(idx) fmt.Println(isFound)
Output: 5 true
func FindLastFunc ¶
FindLastFunc accepts a anonymous func to match
func Generator ¶
func Generator[T any](sl []T) <-chan T
Generator yield-like
Example ¶
intGenerator := Generator[int]([]int{1, 2, 3}) fmt.Println(<-intGenerator) fmt.Println(<-intGenerator) fmt.Println(<-intGenerator) fmt.Println(<-intGenerator)
Output: 1 2 3 0
func Insert ¶
Example ¶
res, _ := Insert[int]([]int{1, 2, 3}, 0, 1) fmt.Println(res)
Output: [1 0 2 3]
func InsertSlice ¶
Example ¶
res, _ := InsertSlice[int]([]int{1, 2, 3}, []int{4, 5}, 1) fmt.Println(res)
Output: [1 4 5 2 3]
func Map ¶
Example ¶
after := Map[int, string]([]int{1, 2, 3, 4, 5}, func(src int) string { return strconv.Itoa(src) }) fmt.Println(after)
Output: [1 2 3 4 5]
func Max ¶
func Max[T generickit.RealNumber](sl []T) T
Example ¶
res := Max[int]([]int{1, 2, 3, 4, 5}) fmt.Println(res)
Output: 5
func Min ¶
func Min[T generickit.RealNumber](sl []T) T
Example ¶
res := Min[int]([]int{1, 2, 3, 4, 5}) fmt.Println(res)
Output: 1
func Reduce ¶
Reduce usually for aggregation, Dst in Number type in order to support operators.
Example ¶
after := Reduce[int, int]([]int{1, 2, 3, 4, 5}, func(src int) int { return src }) fmt.Println(after)
Output: 15
func Reverse ¶
func Reverse[T any](s []T) []T
Reverse in-place
Example ¶
res := Reverse[int]([]int{5, 4, 3, 2, 1}) fmt.Println(res)
Output: [1 2 3 4 5]
func Shrink ¶
func Shrink[T any](src []T) []T
Example ¶
sl := make([]int, 100, 300) sl = Shrink[int](sl) fmt.Println(cap(sl)) sl = make([]int, 300, 480) sl = Shrink[int](sl) fmt.Println(cap(sl))
Output: 150 384
func Sum ¶
func Sum[T generickit.Number](sl []T) T
Example ¶
res := Sum[int]([]int{1, 2, 3, 4, 5}) fmt.Println(res)
Output: 15
Types ¶
type Number ¶
type Number interface { constraints.Integer | constraints.Float }
Click to show internal directories.
Click to hide internal directories.