Documentation
¶
Overview ¶
Contains slice helpers (Map, Filter, Reduce, Fold, Remove, ToMap and ect.).
Index ¶
- func AppendNotNil[T any](slice []*T, elems ...*T) []*T
- func Batch[T any](s []T, size int, f func([]T) error) error
- func Clone[T any](s []T) []T
- func Contains[T comparable](s T, in []T) bool
- func ConvertNumbers[T, V constraints.Number](t []T) []V
- func Count[T any](s []T, f func(T) bool) int
- func Diff[T comparable](s1, s2 []T) []T
- func Each[T any](s []T, f func(v T))
- func Equal[T comparable](s1, s2 []T) bool
- func Filter[T any](s []T, f func(T) bool) []T
- func FilterMap[T1, T2 any](s []T1, filter func(T1) bool, mapper func(T1) T2) []T2
- func FindFirst[T any](items []T, filter func(item T) bool) (T, bool)
- func FitIndex[T any](index int, s []T) int
- func Fold[T comparable](s []T) []T
- func FoldFunc[T any, K comparable](s []T, key func(T) K) []T
- func FromMap[I any, K comparable, T any](items map[K]T, fn func(key K, value T) I) []I
- func Group[T any, K comparable](s []T, key func(T) K) map[K][]T
- func GroupOrder[T any, K comparable](s []T, key func(T) K, less func(s []T, i, j int) bool) map[K][]T
- func Grow[T any](s []T, capacity int) []T
- func HasDuplicates[T comparable](s []T) bool
- func HasDuplicatesFunc[T, K comparable](s []T, key func(T) K) bool
- func Index[T comparable](s []T, v T) int
- func Insert[T any](s []T, pos int, v ...T) []T
- func Intersect[T comparable](s1, s2 []T) []T
- func Last[T constraints.Ordered](s []T) T
- func LastExists[T constraints.Ordered](s []T) (element T, exists bool)
- func Map[T1, T2 any](s []T1, f func(T1) T2) []T2
- func MapFilter[T1, T2 any](s []T1, f func(T1) (T2, bool)) []T2
- func MapFold[T any, K comparable](ss []T, f func(T) K) []K
- func MapIndexed[T1, T2 any](s []T1, f func(int, T1) T2) []T2
- func MapNotNil[T1, T2 any](s []*T1, f func(*T1) T2) []T2
- func Max[T constraints.Ordered](s ...T) T
- func Merge[T any](ss ...[]T) []T
- func MergeSorted[T any](s1 []T, s2 []T, less func(v1, v2 T) bool, limit int) []T
- func MergeSortedTo[T any](dst, src1, src2 []T, less func(v1, v2 T) bool, limit int) []T
- func Min[T constraints.Ordered](s ...T) T
- func New[T any](size int) []*T
- func NewFrom[K, T any](source []K, init func(dst *T, src K)) []*T
- func NewFromFilter[K, T any](source []K, init func(dst *T, src K) bool) []*T
- func NewInit[T any](size int, init func(i int, item *T)) []*T
- func NewInitFilter[T any](size int, init func(i int, item *T) bool) []*T
- func ProcessNotNil[T any](s []*T, f func(*T) error) error
- func Reduce[T, R any](s []T, initializer R, f func(R, T) R) R
- func Remove[T any](s []T, pos int) []T
- func RemoveFunc[T any](s []T, remove func(item T) bool) []T
- func RemoveMany[T any](s []T, pos int, amount int) []T
- func Reverse[T any](s []T)
- func Sort[T constraints.Ordered](s []T)
- func SortDesc[T constraints.Ordered](s []T)
- func SymmetricDiff[T comparable](s1, s2 []T) []T
- func ToMap[I any, K comparable, T any](items []I, fn func(item I) (K, T)) map[K]T
- func Union[T comparable](ss ...[]T) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendNotNil ¶ added in v0.1.0
func AppendNotNil[T any](slice []*T, elems ...*T) []*T
Append not nil elements.
func Batch ¶ added in v0.1.0
Batch split the slice to batches and call the callback function with the every batch.
Example ¶
const size = 4
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
var results [][]int
err := Batch(s, size, func(s []int) error {
results = append(results, s)
return nil
})
fmt.Println(err)
fmt.Println(results)
// with error
err = Batch(s, size, func(s []int) error {
return errors.New("oops")
})
fmt.Println(err)
// empty
results = nil
_ = Batch(nil, size, func(s []int) error {
results = append(results, s)
return nil
})
fmt.Println(results)
Output: <nil> [[1 2 3 4] [5 6 7 8] [9]] oops []
func Contains ¶ added in v0.1.0
func Contains[T comparable](s T, in []T) bool
Contains checks that the slice contains the specified value.
func ConvertNumbers ¶ added in v0.1.0
func ConvertNumbers[T, V constraints.Number](t []T) []V
ConvertNumbers converts slices of numbers of one number type to slices of another numbers type.
func Count ¶ added in v0.1.0
Count values.
Example ¶
fmt.Println(Count([]string{"fish", "crab", "", "octopus", "", "squid", "", ""}, func(v string) bool { return v == "" }))
Output: 4
func Filter ¶
Filter filters values from a slice using a filter function. It returns a new slice with only the elements of s for which f returned true.
func FilterMap ¶ added in v0.1.0
FilterMap first filters values from a slice using a filter function and then map them. It returns a new slice with only the mapped elements of s for which filter returned true.
func FitIndex ¶ added in v0.1.0
FitIndex fits the index into slice range. If the slice is empty the result will be -1.
Example ¶
fmt.Println(FitIndex(-1, []int{1, 1, 1}))
fmt.Println(FitIndex(11, []int{1, 1, 1}))
fmt.Println(FitIndex(1, []int{1, 1, 1}))
fmt.Println(FitIndex(1, []int{}))
Output: 0 2 1 -1
func FoldFunc ¶ added in v0.1.0
func FoldFunc[T any, K comparable](s []T, key func(T) K) []T
FoldFunc deduplicates items by keys.
func FromMap ¶ added in v0.1.0
func FromMap[I any, K comparable, T any](items map[K]T, fn func(key K, value T) I) []I
FromMap extract slice from map.
func Group ¶
func Group[T any, K comparable](s []T, key func(T) K) map[K][]T
Group items.
Example ¶
type data struct {
Key int
Value int
}
s := []data{{Key: 4, Value: 5}, {Key: 4, Value: 4}, {Key: 2, Value: 3}, {Key: 1, Value: 2}, {Key: 1, Value: 1}}
m := Group(s, func(v data) int { return v.Key })
fmt.Println(m[4])
fmt.Println(m[2])
fmt.Println(m[1])
Output: [{4 5} {4 4}] [{2 3}] [{1 2} {1 1}]
func GroupOrder ¶
func GroupOrder[T any, K comparable](s []T, key func(T) K, less func(s []T, i, j int) bool) map[K][]T
GroupOrder items.
Example ¶
type data struct {
Key int
Value int
}
s := []data{{Key: 4, Value: 5}, {Key: 4, Value: 4}, {Key: 2, Value: 3}, {Key: 1, Value: 2}, {Key: 1, Value: 1}}
m := GroupOrder(s, func(v data) int { return v.Key }, func(s []data, i, j int) bool { return s[i].Value < s[j].Value })
fmt.Println(m[4])
fmt.Println(m[2])
fmt.Println(m[1])
Output: [{4 4} {4 5}] [{2 3}] [{1 1} {1 2}]
func HasDuplicates ¶ added in v0.1.0
func HasDuplicates[T comparable](s []T) bool
HasDuplicates checks if the slice has deduplicates.
Example ¶
fmt.Println(HasDuplicates([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 31}))
fmt.Println(HasDuplicates([]int{1, 3, 4, 7, 0, 5, 100, 3, 30, 31}))
fmt.Println(HasDuplicates([]int{1, 1}))
fmt.Println(HasDuplicates([]int{1}))
fmt.Println(HasDuplicates([]int{}))
fmt.Println(HasDuplicates([]int(nil)))
Output: false true true false false false
func HasDuplicatesFunc ¶ added in v0.1.0
func HasDuplicatesFunc[T, K comparable](s []T, key func(T) K) bool
HasDuplicatesFunc checks if the slice has deduplicates.
Example ¶
fmt.Println(HasDuplicatesFunc([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 31}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1, 3, 4, 7, 0, 5, 100, 3, 30, 31}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1, 1}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int(nil), func(i int) int { return i }))
Output: false true true false false false
func Index ¶
func Index[T comparable](s []T, v T) int
Index returns the index of the first instance of v in s, or -1 if v is not present in s.
func Intersect ¶
func Intersect[T comparable](s1, s2 []T) []T
Intersect returns m1 values that keys is contained in m2.
func Last ¶ added in v0.1.3
func Last[T constraints.Ordered](s []T) T
Last returns last element. It panics if slice has zero length.
Example ¶
fmt.Println(Last([]int{10, 20, 30}))
Output: 30
func LastExists ¶ added in v0.1.3
func LastExists[T constraints.Ordered](s []T) (element T, exists bool)
LastExists returns last element.
Example ¶
fmt.Println(LastExists([]int{10, 20, 30}))
fmt.Println(LastExists([]int{}))
fmt.Println(LastExists[int](nil))
Output: 30 true 0 false 0 false
func Map ¶
func Map[T1, T2 any](s []T1, f func(T1) T2) []T2
Map turns a []T1 to a []T2 using a mapping function. This function has two type parameters, T1 and T2. This works with slices of any type.
func MapFilter ¶ added in v0.1.0
MapFilter turns a []T1 to a []T2 using a mapping function, Values is not placed to result slice when the mapping function return false. The resulting slice may have a smaller size than the original.
func MapFold ¶ added in v0.1.11
func MapFold[T any, K comparable](ss []T, f func(T) K) []K
MapFold turns a []T to a []K using a mapping function. The result will be contain unique values.
Example ¶
fmt.Println(MapFold([]int{0, 1, 2, 3, 4, 5}, strconv.Itoa))
fmt.Println(MapFold([]int{0, 1, 2, 1, 3, 4, 5, 0}, strconv.Itoa))
fmt.Println(MapFold(nil, strconv.Itoa))
Output: [0 1 2 3 4 5] [0 1 2 3 4 5] []
func MapIndexed ¶ added in v0.1.0
MapIndexed turns a []T1 to a []T2 using a mapping function. This function has two type parameters, T1 and T2. This works with slices of any type.
func MapNotNil ¶ added in v0.1.0
func MapNotNil[T1, T2 any](s []*T1, f func(*T1) T2) []T2
MapNotNil turns a []*T1 to a []T2 using a mapping function, exclude nil values. This works with slices of any type. The resulting slice may have a smaller size than the original.
func Max ¶ added in v0.0.2
func Max[T constraints.Ordered](s ...T) T
Max returns the maximum value of the slice.
Example ¶
fmt.Println(Max(10, 0, 1)) fmt.Println(Max(1))
Output: 10 1
func Merge ¶ added in v0.1.0
func Merge[T any](ss ...[]T) []T
Merge slices into one.
Example ¶
fmt.Println(Merge([]int{1, 2, 3, 4}, []int{4, 5, 6}, nil, []int{5, 7}))
Output: [1 2 3 4 4 5 6 5 7]
func MergeSorted ¶ added in v0.1.0
MergeSorted merges two sorted slices to one new sorted. If limit is zero then result size is not limited.
Example ¶
fmt.Println(MergeSorted([]int{1, 3, 4, 7}, []int{2, 3, 6, 8}, func(v1, v2 int) bool { return v1 < v2 }, 0))
fmt.Println(MergeSorted([]int{1, 3, 4, 7}, []int{2, 3, 6, 8}, func(v1, v2 int) bool { return v1 < v2 }, 3))
Output: [1 2 3 3 4 6 7 8] [1 2 3]
func MergeSortedTo ¶ added in v0.1.0
MergeSortedTo merges two sorted slices to one sorted. Dst may be overwritten if capacity is enough. s1 or s2 may be used as dst (for collecting values from many slices to one). If limit is zero then result size is not limited.
Example ¶
s1, s2 := []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
s1, s2 = []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 3))
s1, s2 = []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 20))
s1, s2 = nil, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
s1, s2 = []int{2, 3, 6, 8}, nil
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
Output: [1 2 3 3 4 6 7 8] [1 2 3] [1 2 3 3 4 6 7 8] [2 3 6 8] [2 3 6 8]
func Min ¶ added in v0.0.2
func Min[T constraints.Ordered](s ...T) T
Min returns the minimum value of the slice.
Example ¶
fmt.Println(Min(10, 0, 1)) fmt.Println(Min(1))
Output: 0 1
func NewFrom ¶ added in v0.1.0
func NewFrom[K, T any](source []K, init func(dst *T, src K)) []*T
NewFrom allocate fast the slice of pointers of specified type and initializes it.
func NewFromFilter ¶ added in v0.1.0
NewFromFilter allocate fast the slice of pointers of specified type and initializes it. It skips output if the init function return false.
func NewInit ¶ added in v0.1.0
NewInit allocate fast the slice of pointers of specified type and initializes it.
func NewInitFilter ¶ added in v0.1.0
NewInitFilter allocate fast the slice of pointers of specified type and initializes it.
func ProcessNotNil ¶ added in v0.1.0
ProcessNotNil process not nil elements.
func Reduce ¶
func Reduce[T, R any](s []T, initializer R, f func(R, T) R) R
Reduce reduces a []T to a single value using a reduction function.
func Remove ¶ added in v0.1.0
Remove the element at position. It returns the same slice with reduced size.
func RemoveFunc ¶ added in v0.1.0
Remove elements from the slice. It returns the same slice with reduced size.
Example ¶
fmt.Println(RemoveFunc([]int{2, 3, 4, 7, 0, 5, 100, 15, 30, 31}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 32}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1, 1}, func(i int) bool { return i%2 == 1 }))
fmt.Println(RemoveFunc([]int{1, 1}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1}, func(i int) bool { return i%2 == 1 }))
fmt.Println(RemoveFunc([]int{}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int(nil), func(i int) bool { return i%2 == 0 }))
Output: [3 7 5 15 31] [1 3 7 5 15] [] [1 1] [1] [] [] []
func RemoveMany ¶ added in v0.1.0
Remove the elements at position. It returns the same slice with reduced size.
func Sort ¶ added in v0.1.0
func Sort[T constraints.Ordered](s []T)
Sort slices ascending.
Example ¶
s := []int{10, 120, 30}
Sort(s)
fmt.Println(s)
Output: [10 30 120]
func SortDesc ¶ added in v0.1.0
func SortDesc[T constraints.Ordered](s []T)
Sort slices descending.
Example ¶
s := []int{10, 120, 30}
SortDesc(s)
fmt.Println(s)
Output: [120 30 10]
func SymmetricDiff ¶
func SymmetricDiff[T comparable](s1, s2 []T) []T
SymmetricDiff gets the symmetric difference of two sets and gives a set of elements, which are in either of the sets and not in their intersection.
func ToMap ¶ added in v0.1.0
func ToMap[I any, K comparable, T any](items []I, fn func(item I) (K, T)) map[K]T
ToMap convert slice to map.
func Union ¶ added in v0.1.0
func Union[T comparable](ss ...[]T) []T
Union slices into one. Duplicates is removed. Order of items in slices is preserved.
Example ¶
fmt.Println(Union([]int{1, 2, 3, 4}, []int{4, 5, 6}, []int{5, 7}))
fmt.Println(Union([]int{4, 5, 6}, []int{1, 2, 3, 4}, []int{5, 7}))
fmt.Println(Union(nil, []int{4, 5, 6}, nil))
fmt.Println(Union([]string(nil)))
Output: [1 2 3 4 5 6 7] [4 5 6 1 2 3 7] [4 5 6] []
Types ¶
This section is empty.