Documentation
¶
Index ¶
- func All[S ~[]T, T ~bool](s S) bool
- func AllFunc[S ~[]T, T any](s S, cmd func(T) bool) bool
- func Any[S ~[]T, T ~bool](s S) bool
- func AnyFunc[S ~[]T, T any](s S, cmd func(T) bool) bool
- func Filter[S ~[]T, T any](s S, filter func(T) bool) []T
- func First[S ~[]T, T any](s S) T
- func FirstFunc[S ~[]T, T any](s S, cmd func(T) bool) T
- func FirstOrDefault[S ~[]T, T any](s S) T
- func FirstOrDefaultFunc[S ~[]T, T any](s S, cmd func(T) bool) T
- func ForEach[S ~[]T, T any](s S, cmd func(T))
- func ForEachWithErr[S ~[]T, T any](s []T, cmd func(T) error) error
- func IsEmpty[S ~[]T, T any](s S) bool
- func Last[S ~[]T, T any](s S) T
- func LastFunc[S ~[]T, T any](s S, cmd func(T) bool) T
- func LastOrDefault[S ~[]T, T any](s S) T
- func LastOrDefaultFunc[S ~[]T, T any](s S, cmd func(T) bool) T
- func Multiply[S ~[]T, T MathOps](s S) T
- func MultiplyFunc[S ~[]T, T any, R MathOps](s S, cmd func(T) R) R
- func NotEmpty[S ~[]T, T any](s S) bool
- func Reduce[S ~[]T, T, R any](s S, init R, cmd func(R, T) R) R
- func Sum[S ~[]T, T MathOps](s S) T
- func SumFunc[S ~[]T, T any, R MathOps](s S, cmd func(T) R) R
- func ToMap[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K]T
- func ToMapBuckets[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K][]T
- func Transform[S ~[]T, T, R any](source S, applyFunc func(T) R) []R
- func Update[S ~[]T, T any](source S, applyFunc func(T) T)
- type MathOps
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶ added in v1.0.0
The All function accept slice with bool elements and return true if all are true.
func AllFunc ¶ added in v1.0.0
The AllFunc function returns true if cmd function return true for all elements. If collection is empty, true returns. So you may need to check collection on empty. Right way to use
if slices.AllFunc([]int{2,4,6}, func(val int)bool { return val&1 == 0 } ) { ...
func Any ¶ added in v1.0.0
The Any function accept slice with bool elements and return true if one or more are true.
func AnyFunc ¶ added in v1.0.0
The AnyFunc function returns true if cmd function return true for one or more. If collection is empty, false returns. So you may need to check collection on empty. Right way to use
if slices.AnyFunc([]int{1,2,3,4}, func(val int)bool { return val&1 == 0 } ) { ...
func Filter ¶
The filter function select elements of a slice that satisfy to filter function. It creates new slice with same size and append only satisfing elements. If you wnaht to remove unused cpacity from slice, use slices.Clip Filter returns the updated slice. It is therefore necessary to store the result of filtering, often in the variable holding the slice itself:
slice = slices.filter(slice, filterFunc)
func First ¶
func First[S ~[]T, T any](s S) T
The First function returns first element of slice. If collection is empty, function panic You should use FirstOrDefault function if collection can be empty
func FirstFunc ¶
The FirstFunc function returns first element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function panic You should use FirstOrDefaultFunc function to avoid panics
func FirstOrDefault ¶
func FirstOrDefault[S ~[]T, T any](s S) T
The FirstOrDefault function returns first element of slice. If collection is empty, function returns default value
func FirstOrDefaultFunc ¶
The FirstOrDefaultFunc function returns first element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function returns default value
func ForEach ¶
func ForEach[S ~[]T, T any](s S, cmd func(T))
The ForEach function exec any func for each element of slice. !!!Pay attention, if you want to change slice non ref values you should use Transform or Update This example won't update source slice, just take you memory and cpu time:
s := []int{1,2,3}
slices.ForEach(s, func(i int){ i+=2 })
Right way to use this function
s := []int{1,2,3}
slices.ForEach(s, func(i int){ logger.Log("Element", i) })
func ForEachWithErr ¶
The ForEachWithErr close to ForEach function. It exec any func for each element of slice unitl error returns. Right way to use this function
s := []int{1,2,3}
slices.ForEach(s, func(i int) error {
if i == 5{
return "Unexpected value"
}
logger.Log("Element", i)
return nil
})
func IsEmpty ¶ added in v1.0.0
The IsEmpty function checks if collection is empty. It returns true if collection is empty
func Last ¶
func Last[S ~[]T, T any](s S) T
The Last function returns last element of slice. If collection is empty, function panic You should use LastOrDefault function if collection can be empty
func LastFunc ¶
The LastFunc function returns last element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function panic You should use FirstOrDefaultFunc function to avoid panics
func LastOrDefault ¶
func LastOrDefault[S ~[]T, T any](s S) T
The LastOrDefault function returns last element of slice. If collection is empty, function returns default value
func LastOrDefaultFunc ¶
The LastOrDefaultFunc function returns last element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function returns default value
func Multiply ¶
func Multiply[S ~[]T, T MathOps](s S) T
The Multiply function multiply each of collection element to single value.
func MultiplyFunc ¶
The Multiply function multiply function results for each collection element. Right way to use.
val = slices.MultiplyFunc([]string{"aaa","bb"}, func(r string) int {return len(r)})
Pay attention! It's just an example.
func NotEmpty ¶ added in v1.0.0
The NotEmpty function checks if collection is empty. It returns true if collection is not empty
func Reduce ¶
func Reduce[S ~[]T, T, R any](s S, init R, cmd func(R, T) R) R
The Reduce function apply each of collection element to single value. Right way to use. For example to join all values in slice in raw
val = slices.Reduce([]int{1,2,3}, "", func(r string, val int) string {return r + itoa.Itoa(val)})
Pay attention! It's just an example, Not recommended way to join values in string raw
func Sum ¶
func Sum[S ~[]T, T MathOps](s S) T
The Sum function sum each of collection element to single value.
func SumFunc ¶
The SumFunc function sum function results for each collection element. Right way to use.
val = slices.SumFunc([]string{"aaa","bb"}, func(r string) int {return len(r)})
Pay attention! It's just an example.
func ToMap ¶
func ToMap[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K]T
The ToMap function convert slice to map with keys calculated by keySelector function It creates new map with same size as slice. keySelector func must returns comparable type. If slice contain several element with equal calculated keys, value will be overriden. You can use ToMapBuckets to avoid it. Rigth way to use function
slice = slices.ToMap([]int{1,2,3}, func(val int) string { return itoa.Itoa(val) })
func ToMapBuckets ¶
func ToMapBuckets[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K][]T
The ToMapBuckets function convert slice to map with keys calculated by keySelector function It creates new map with same size as slice. keySelector func must returns comparable type. If slice contain several element with equal calculated keys, all values will be saved in slice. So, it can be used as Group function. Rigth way to use function
//First example
slice = slices.ToMapBuckets([]int{1,2,3,2,1}, func(val int) string { return itoa.Itoa(val) })
//Second example
events := []struct{d date, name string}{ ... }
eventsCalendar = slices.ToMapBuckets(events, func(val struct{d date, name string}) date { return val.date })
func Transform ¶
func Transform[S ~[]T, T, R any](source S, applyFunc func(T) R) []R
The Transform function convert each element of slice to new one. It creates new slice with same size and append all transformed values. Transform returns the updated slice. It is therefore necessary to store the result of filtering, often in the variable holding the slice itself:
slice := []int{1,2,3,4}
slice = slices.Transform(slice, filterFunc)
It also can return slice with another type
slice := []int{1,2,3,4}
slice1 := slices.Transform(slice, func(val int) string {return itoa.Itoa(val)})
Types ¶
type MathOps ¶
type MathOps interface {
constraints.Integer | constraints.Float | constraints.Complex
}