Index
- func All[T any](slice []T, fn func(v T) bool) bool
- func Any[T any](slice []T, fn func(v T) bool) bool
- func Cartesian[T any](slices ...[]T) (res [][]T)
- func Chunks[T any](slice []T, size int) (chunks [][]T)
- func Contains[T comparable](slice []T, val T) bool
- func ContainsRuntime(slice any, val any) bool
- func Count[T any](slice []T, fn func(v T) bool) int
- func Filter[T any](slice []T, fn func(v T) bool) []T
- func In[T comparable](val T, slice []T) bool
- func InFunc[T comparable](values ...T) func(T) bool
- func InRuntime(val any, slice any) bool
- func Index[T any](slice []T, fn func(v T) bool) int
- func Insert[T any](slice []T, index int, value T) []T
- func Last[T any](slice []T) T
- func Limit[T any](slice []T, limit int) []T
- func Map[T1 any, T2 any](slice []T1, fn func(v T1) T2) []T2
- func MapIndexed[T1 any, T2 any](slice []T1, fn func(i int, v T1) T2) []T2
- func NotInFunc[T comparable](values ...T) func(T) bool
- func Pop[T any](slice []T, index ...int) ([]T, T)
- func Range(from, to int) []int
- func Unique[T1 any, T2 comparable](slice []T1, fn func(v T1) T2) []T1
func All
func All[T any](slice []T, fn func(v T) bool) bool
All ensures that all values from a given slice satisfies a given condition.
Usage:
All([]int{1, 2, 3}, func(v int) bool { return v > 0 }) // true
func Any
func Any[T any](slice []T, fn func(v T) bool) bool
Any ensures that at least one value from a given slice satisfies a given condition.
Usage:
Any([]int{-1, 0, 1}, func(v int) bool { return v < 0 }) // true
func Cartesian
func Cartesian[T any](slices ...[]T) (res [][]T)
Cartesian makes a product of two or more sets.
Usage:
Cartesian([]int{1, 2}, []int{3, 4}) // [[1 3] [1 4] [2 3] [2 4]]
func Chunks
func Chunks[T any](slice []T, size int) (chunks [][]T)
Chunks generates a chunks with a given size from a given slice.
Usage:
Chunks([]int{1, 2, 3, 4}, 2) // [][]int{ []int{1, 2}, []int{3, 4} }
func Contains
func Contains[T comparable](slice []T, val T) bool
Contains checks if a value is in a slice.
Usage:
Contains([]int{1, 2, 3}, 1) // true
Contains([]int{1, 2, 3}, 4) // false
func ContainsRuntime
func ContainsRuntime(slice any, val any) bool
ContainsRuntime is a runtime version of Contains.
func Count
func Count[T any](slice []T, fn func(v T) bool) int
func Filter
func Filter[T any](slice []T, fn func(v T) bool) []T
Filter returns filtered slice according to the given function.
Usage:
Filter([]int{1, 2, 3}, func(v int) bool { return v < 3 }) // []int{1, 2}
func In
func In[T comparable](val T, slice []T) bool
Deprecated: use Contains() instead. It was deprecated because of the inconsistency in argument order between this function and the rest of the functions in the package.
Usage:
In(1, []int{1, 2, 3}) // true
func InFunc
func InFunc[T comparable](values ...T) func(T) bool
InFunc returns a function that checks if the given value is in the given top-level values slice. Works only for comparable types. Useful for filtering slices with slice.Filter.
Usage:
slice.Filter([]int{1, 2, 3, 3, 4, 5}, slice.InFunc(1, 2, 3)) // []int{1, 2, 3, 3}
func InRuntime
func InRuntime(val any, slice any) bool
Deprecated: use ContainsRuntime() instead. It was deprecated because of the inconsistency in argument order between this function and the rest of the functions in the package.
func Index
func Index[T any](slice []T, fn func(v T) bool) int
Index finds element index according to the given function. If nothing found, returns -1.
Usage:
Index([]int{1, 2, 3}, func(v int) bool { return v > 2 }) // 2
func Insert
func Insert[T any](slice []T, index int, value T) []T
Insert injects a provided value into slice on the given index.
Usage:
Insert([]string{"b", "c"}, 0, "a") // []string{"a", "b", "c"}
func Last
func Last[T any](slice []T) T
Last takes a last element of a given slice. In that way, you don't need to mess with len(slice)-1.
Usage:
Last([]string{"a", "b", "c"}) // "c"
func Limit
func Limit[T any](slice []T, limit int) []T
Limit makes a slice subset if bigger than a given limit.
Usage:
Limit([]string{"a", "b", "c"}, 2) // []string{"a", "b"}
func Map
func Map[T1 any, T2 any](slice []T1, fn func(v T1) T2) []T2
Map returns a new slice with the results of applying the given function to each element in the given slice.
Usage:
Map([]string{"asd", "qwe"}, func(v string) int { return len(v) }) // []int{3, 3}
func MapIndexed
func MapIndexed[T1 any, T2 any](slice []T1, fn func(i int, v T1) T2) []T2
MapIndexed returns a new slice with the results of applying the given function to each element in the given slice. Difference from Map is that the function receives the index of the element as the first argument.
func NotInFunc
func NotInFunc[T comparable](values ...T) func(T) bool
NotInFunc returns a function that checks if the given value is not in the given top-level values slice. Works only for comparable types. Useful for filtering slices with slice.Filter.
Usage:
slice.Filter([]int{1, 2, 3, 3, 4, 5}, slice.NotInFunc(1, 2, 3)) // []int{4, 5}
func Pop
func Pop[T any](slice []T, index ...int) ([]T, T)
Pop takes an last element from a slice (with deletion), or with a given index.
Usage:
a := []int{1, 2, 3}
b := Pop(a) // 3
fmt.println(a) // []int{1, 2}
func Range
func Range(from, to int) []int
Range returns a new slice of integers in the given range (from, to).
Usage:
Range(1, 5) // []int{1, 2, 3, 4, 5}
func Unique
func Unique[T1 any, T2 comparable](slice []T1, fn func(v T1) T2) []T1
Unique returns a new slice with the unique slice values. Comparable value is defined by a given function.
Usage:
Unique([]int{1, 2, 2, 3}, func(v int) int { return v }) // []int{1, 2, 3}
Generated by gomarkdoc