Documentation ¶
Index ¶
- func All[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool
- func Any[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool
- func Chunk[S ~[]T, T any](slice S, chunkSize int) []S
- func Concat[S ~[]T, T any](slices ...S) S
- func CountBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]int
- func Difference[S ~[]T, T comparable](slice S, others ...S) S
- func DifferenceBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U, others ...S) S
- func DifferenceWith[S ~[]T, T any](slice S, comparator func(T, T) bool, others ...S) S
- func Drop[S ~[]T, T any](slice S, n int) S
- func DropRight[S ~[]T, T any](slice S, n int) S
- func DropRightWhile[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func DropWhile[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func Each[S ~[]T, T any](slice S, iteratee func(value T, index int, slice S))
- func EachRight[S ~[]T, T any](slice S, iteratee func(value T, index int, slice S))
- func Every[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool
- func Fill[S ~[]T, T any](slice S, value T, start int, end int)
- func Filter[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func Find[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) (result T)
- func FindIndex[S ~[]T, T any](slice S, predicate func(T) bool) int
- func FindLastIndex[S ~[]T, T any](slice S, predicate func(T) bool) int
- func FlatMap[S ~[]T, T any, U any](slice S, iteratee func(value T, index int, slice S) []U) []U
- func GroupBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]S
- func Includes[S ~[]T, T comparable](slice S, value T) bool
- func IndexOf[S ~[]T, T comparable](slice S, value T) int
- func Intersection[S ~[]T, T comparable](slices ...S) S
- func IntersectionBy[S ~[]T, T any, U comparable](iteratee func(T) U, others ...S) S
- func IntersectionWith[S ~[]T, T any](comparator func(T, T) bool, slices ...S) S
- func Join[S ~[]T, T any](slice S, separator string) string
- func KeyBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]T
- func LastIndexOf[S ~[]T, T comparable](slice S, value T) int
- func Map[S ~[]T, T any, U any](slice S, iteratee func(T) U) []U
- func Nth[S ~[]T, T any](slice S, n int) T
- func Partition[S ~[]T, T any](slice S, predicate func(T) bool) (truths S, falsehoods S)
- func Pull[S ~[]T, T comparable](slice S, values ...T) S
- func PullAll[S ~[]T, T comparable](slice S, values []T) S
- func PullAllBy[S ~[]T, T any, U comparable](slice S, values []T, iteratee func(T) U) S
- func PullAllWith[S ~[]T, T any](slice S, values []T, comparator func(T, T) bool) S
- func PullAt[S ~[]T, T comparable](slice S, indexes ...int) S
- func Reduce[S ~[]T, T any, U any](slice S, iteratee func(acc U, value T, index int, slice S) U, accumulator U) U
- func ReduceRight[S ~[]T, T any, U any](slice S, iteratee func(acc U, value T, index int, slice S) U, accumulator U) U
- func Reject[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func Remove[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func Reverse[S ~[]T, T any](slice S) S
- func Some[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool
- func SortedIndex[S ~[]T, T constraints.Ordered](slice S, value T) int
- func SortedIndexBy[S ~[]T, T any, U constraints.Ordered](slice S, value T, iteratee func(T) U) int
- func SortedIndexOf[S ~[]T, T constraints.Ordered](slice S, value T) int
- func SortedLastIndex[S ~[]T, T constraints.Ordered](slice S, value T) int
- func SortedLastIndexBy[S ~[]T, T any, U constraints.Ordered](slice S, value T, iteratee func(T) U) int
- func SortedLastIndexOf[S ~[]T, T constraints.Ordered](slice S, value T) int
- func Take[S ~[]T, T any](slice S, n int) S
- func TakeRight[S ~[]T, T any](slice S, n int) S
- func TakeRightWhile[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func TakeWhile[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) S
- func Union[S ~[]T, T comparable](slices ...S) S
- func UnionBy[S ~[]T, T any, U comparable](iteratee func(T) U, slices ...S) S
- func UnionWith[S ~[]T, T any](comparator func(T, T) bool, sliceList ...S) S
- func Uniq[S ~[]T, T comparable](slice S) S
- func UniqBy[S ~[]T, T any, U comparable](iteratee func(T) U, slice S) S
- func UniqWith[S ~[]T, T any](comparator func(T, T) bool, slice S) S
- func Without[S ~[]T, T comparable](slice S, values ...T) S
- func Xor[S ~[]T, T comparable](slices ...S) S
- func XorBy[S ~[]T, T any, U comparable](iteratee func(T) U, slices ...S) S
- func XorWith[S ~[]T, T any](comparator func(T, T) bool, sliceList ...S) S
Examples ¶
- Chunk
- Concat
- CountBy
- Difference
- DifferenceBy
- DifferenceWith
- Drop
- DropRight
- DropRightWhile
- DropWhile
- Each
- EachRight
- Every
- Fill
- Filter
- Find
- FindIndex
- FindLastIndex
- FlatMap
- GroupBy
- Includes
- IndexOf
- Intersection
- IntersectionBy
- IntersectionWith
- Join
- KeyBy
- LastIndexOf
- Map
- Nth
- Partition
- Pull
- PullAll
- PullAllBy
- PullAllWith
- PullAt
- Reduce
- ReduceRight
- Reject
- Remove
- Reverse
- Some
- SortedIndex
- SortedIndexBy
- SortedIndexOf
- SortedLastIndex
- SortedLastIndexBy
- SortedLastIndexOf
- Take
- TakeRight
- TakeRightWhile
- TakeWhile
- Union
- UnionBy
- UnionWith
- Uniq
- UniqBy
- UniqWith
- Without
- Xor
- XorBy
- XorWith
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if the given `predicate` returns true for every element of the given slice.
func Any ¶
Any return true if the given `predicate` returns true for any element of the given slice.
func Chunk ¶
Chunk splits the given slice into smaller slices, each the length of `chunkSize`. If the slice cannot be split evenly, the last chunk will have the remaining elements.
Example ¶
fmt.Println(Chunk([]int{1, 2, 3, 4}, 2)) fmt.Println(Chunk([]int{1, 2, 3, 4}, 3))
Output: [[1 2] [3 4]] [[1 2 3] [4]]
func Concat ¶
func Concat[S ~[]T, T any](slices ...S) S
Concat combines all the elements from all the given slices into a single slice.
Example ¶
fmt.Println(Concat([]int{1, 2}, []int{3})) fmt.Println(Concat([]int{1, 2}, []int{3}, []int{4, 5, 6}))
Output: [1 2 3] [1 2 3 4 5 6]
func CountBy ¶
func CountBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]int
CountBy creates a map composed of keys generated from the results of running each element of the slice through `iteratee`. The corresponding value of each key is the number of times the key was returned by `iteratee`.
Example ¶
fmt.Println(CountBy([]float64{6.1, 4.2, 6.3}, math.Floor))
Output: map[4:1 6:2]
func Difference ¶
func Difference[S ~[]T, T comparable](slice S, others ...S) S
Difference returns a list of items present in `slice` that are *not* present in any of the `others` slices. The comparison is performed with `==`.
Example ¶
fmt.Println(Difference([]int{2, 1}, []int{2, 3})) fmt.Println(Difference([]int{1, 2, 3, 4, 5, 6, 7}, []int{0, 1, 2}, []int{5, 6, 7, 8}))
Output: [1] [3 4]
func DifferenceBy ¶
func DifferenceBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U, others ...S) S
DifferenceBy returns a list of items present in `slice` that are *not* present in any of the `others` slices, with the comparison made by passing items into the `iteratee` function and checking `==` on the result. This allows changing the way the item is viewed for comparison.
Example ¶
fmt.Println(DifferenceBy([]float64{2.1, 1.2}, math.Floor, []float64{2.3, 3.4})) fmt.Println(DifferenceBy([]float64{-2, -1, 0, 1, 2}, math.Abs, []float64{1}, []float64{-2}))
Output: [1.2] [0]
func DifferenceWith ¶
DifferenceWith returns a slice of items present in `slice` that are *not* present in any of the `others` slices, with the comparison made using the given `comparator`.
Example ¶
array := []map[string]int{{"a": 1}, {"b": 2}} others := []map[string]int{{"a": 1}} fmt.Println(DifferenceWith(array, func(x, y map[string]int) bool { return reflect.DeepEqual(x, y) }, others))
Output: [map[b:2]]
func Drop ¶
Drop returns a new slice with `n` elements dropped from the beginning.
Example ¶
fmt.Println(Drop([]int{1, 2, 3}, 1)) fmt.Println(Drop([]int{1, 2, 3}, 2)) fmt.Println(Drop([]int{1, 2, 3}, 5)) fmt.Println(Drop([]int{1, 2, 3}, 0))
Output: [2 3] [3] [] [1 2 3]
func DropRight ¶
DropRight returns a new slice with `n` elements dropped from the end.
Example ¶
fmt.Println(DropRight([]int{1, 2, 3}, 1)) fmt.Println(DropRight([]int{1, 2, 3}, 2)) fmt.Println(DropRight([]int{1, 2, 3}, 5)) fmt.Println(DropRight([]int{1, 2, 3}, 0))
Output: [1 2] [1] [] [1 2 3]
func DropRightWhile ¶
DropRightWhile creates a new slice excluding elements dropped from the end. Elements are dropped until `predicate` returns false.
Example ¶
haystack := []string{"h1", "h2", "h3", "needle", "h4", "h5", "needle", "h6", "h7", "h8"} fmt.Println(DropRightWhile(haystack, func(item string, index int, arr []string) bool { return item != "needle" })) fmt.Println(DropRightWhile([]string{}, func(item string, index int, arr []string) bool { return item != "needle" }))
Output: [h1 h2 h3 needle h4 h5 needle] []
func DropWhile ¶
DropWhile creates a new slice excluding elements dropped from the beginning. Elements are dropped until `predicate` returns false.
Example ¶
haystack := []string{"h1", "h2", "h3", "needle", "h4", "h5", "needle", "h6", "h7", "h8"} fmt.Println(DropWhile(haystack, func(item string, index int, arr []string) bool { return item != "needle" }))
Output: [needle h4 h5 needle h6 h7 h8]
func Each ¶
Each invokes the given `iteratee` for every element in the slice, from left to right.
Example ¶
Each([]int{1, 2, 3}, func(v int, index int, arr []int) { fmt.Println(v) })
Output: 1 2 3
func EachRight ¶
EachRight invokes the given `iteratee` for every element in the slice, from right to left.
Example ¶
EachRight([]int{1, 2, 3}, func(v int, index int, arr []int) { fmt.Println(v) })
Output: 3 2 1
func Every ¶
Every returns true if the given `predicate` returns true for every element of the given slice.
Example ¶
fmt.Println(Every([]int{2, 4, 6, 8}, func(v int, index int, arr []int) bool { return v%2 == 0 })) fmt.Println(Every([]int{}, func(v int, index int, arr []int) bool { return v%2 == 0 })) fmt.Println(Every([]int{1, 2}, func(v int, index int, arr []int) bool { return v%2 == 0 }))
Output: true true false
func Fill ¶
Fill fills elements of `slice` with `value` from `start` up to, but not including `end`.
Example ¶
array := []string{"a", "b", "c", "d"} Fill(array, "*", 1, 3) fmt.Println(array)
Output: [a * * d]
func Filter ¶
Filter iterates over the elements of `slice`, returning a slice of all elements that the `predicate` returns true for.
Example ¶
fmt.Println(Filter([]int{1, 2, 3, 4, 5, 6}, func(v int, index int, arr []int) bool { return v%2 == 0 }))
Output: [2 4 6]
func Find ¶
Find iterates over the elements of `slice`, returning the first element that `predicate` returns true for.
Example ¶
fmt.Println(Find([]int{1, 2, 3, 4}, func(value int, index int, arr []int) bool { return value > 2 }))
Output: 3
func FindIndex ¶
FindIndex returns the index of the first element for which the `predicate` returns true.
Example ¶
fmt.Println(FindIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "x" })) fmt.Println(FindIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "a" })) fmt.Println(FindIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "d" })) fmt.Println(FindIndex([]string{"a", "a", "b", "b"}, func(t string) bool { return t == "b" }))
Output: -1 0 3 2
func FindLastIndex ¶
FindLastIndex returns the index of the last element of which the `predicate` returns true.
Example ¶
fmt.Println(FindLastIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "x" })) fmt.Println(FindLastIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "a" })) fmt.Println(FindLastIndex([]string{"a", "b", "c", "d"}, func(t string) bool { return t == "d" })) fmt.Println(FindLastIndex([]string{"a", "a", "b", "b"}, func(t string) bool { return t == "a" }))
Output: -1 0 3 1
func FlatMap ¶
FlatMap creates a flattened slice of values by running each element in `slice` through `iteratee` and flattening the mapped results.
Example ¶
fmt.Println(FlatMap([]int{1, 2}, func(value int, index int, arr []int) []int { return []int{value, value} }))
Output: [1 1 2 2]
func GroupBy ¶
func GroupBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]S
GroupBy creates a map composed of keys generated from the results of running each element of `slice` through `iteratee`. The order of the grouped values is determined by the order that they occur in `slice`. The corresponding value of each key is a slice of elements responsible for generating the key.
Example ¶
fmt.Println(GroupBy([]float64{6.1, 4.2, 6.3}, math.Floor))
Output: map[4:[4.2] 6:[6.1 6.3]]
func Includes ¶
func Includes[S ~[]T, T comparable](slice S, value T) bool
Includes checks if `value` is in `slice`. Equality is checked with `==`.
Example ¶
fmt.Println(Includes([]int{1, 2, 3}, 1)) fmt.Println(Includes([]int{1, 2, 3}, 42))
Output: true false
func IndexOf ¶
func IndexOf[S ~[]T, T comparable](slice S, value T) int
IndexOf returns the index at which the first occurrence of `value` is found in `slice`. Returns `-1` if not found.
Example ¶
fmt.Println(IndexOf([]string{"a", "b", "c"}, "x")) fmt.Println(IndexOf([]string{"a", "b", "c"}, "a")) fmt.Println(IndexOf([]string{"a", "b", "b", "c"}, "b"))
Output: -1 0 1
func Intersection ¶
func Intersection[S ~[]T, T comparable](slices ...S) S
Intersection returns a slice of unique values that are included in all given slices. The order of the result values are determined by the first slice.
Example ¶
fmt.Println(Intersection([]int{2, 1}, []int{2, 3})) fmt.Println(Intersection([]int{2, 1}, []int{2, 3}, []int{8, 2})) fmt.Println(Intersection([]int{2, 1, 2, 2, 1}, []int{1, 2, 3, 2}, []int{8, 1, 2, 2})) fmt.Println(Intersection([]int{}, []int{}))
Output: [2] [2] [2 1] []
func IntersectionBy ¶
func IntersectionBy[S ~[]T, T any, U comparable](iteratee func(T) U, others ...S) S
IntersectionBy returns a slice of unique values that are included in all given slices, with comparison happening on the result of the `iteratee` function. The order of the result values are determined by the first slice.
Example ¶
fmt.Println(IntersectionBy(math.Floor, []float64{2.1, 1.2}, []float64{2.3, 3.4})) fmt.Println(IntersectionBy(math.Floor, []float64{2.1, 1.2, 2.4}, []float64{2.3, 3.4}))
Output: [2.1] [2.1]
func IntersectionWith ¶
IntersectionWith returns a slice of unique values that are included in all given slice, with comparison happening inside the given `comparator`. The order of the result values are determined by the first slice.
Example ¶
fmt.Println(IntersectionWith(func(x, y float64) bool { return math.Floor(x) == math.Floor(y) }, []float64{2.1, 1.2}, []float64{2.3, 3.4})) fmt.Println(IntersectionWith(func(x, y float64) bool { return math.Floor(x) == math.Floor(y) }, []float64{2.1, 1.2, 2.4}, []float64{2.3, 3.4}))
Output: [2.1] [2.1]
func Join ¶
Join concatenates all the elements of the slice into a string separated by `separator`. `fmt.Sprint` is used for to get the string representation of the given value, so mixed types are possible with `[]any`.
Example ¶
fmt.Println(Join([]string{"Hello", "World"}, " ")) fmt.Println(Join([]int{2022, 1, 1}, "/")) fmt.Println(Join([]any{1, "January", 2022}, "-"))
Output: Hello World 2022/1/1 1-January-2022
func KeyBy ¶
func KeyBy[S ~[]T, T any, U comparable](slice S, iteratee func(T) U) map[U]T
KeyBy creates a map composed of keys generated from the results of running each element of `slice` through `iteratee`. The corresponding value of each key is the last element responsible for generating the key.
Example ¶
fmt.Println(KeyBy([]float64{6.1, 4.2, 6.3}, math.Floor))
Output: map[4:4.2 6:6.3]
func LastIndexOf ¶
func LastIndexOf[S ~[]T, T comparable](slice S, value T) int
LastIndexOf returns the index at which the last occurrence of `value` is found in `slice`. Returns `-1` if not found.
Example ¶
fmt.Println(LastIndexOf([]string{"a", "b", "c"}, "x")) fmt.Println(LastIndexOf([]string{"a", "b", "c"}, "a")) fmt.Println(LastIndexOf([]string{"a", "b", "b", "c"}, "b"))
Output: -1 0 2
func Map ¶
Map creates a slice of values by running each element in `slice` through `iteratee`.
Example ¶
fmt.Println(Map([]int{4, 8}, func(n int) int { return n * n }))
Output: [16 64]
func Nth ¶
Nth gets the element at index `n` of the `slice`. If `n` is negative, the nth element from the end is returned.
Example ¶
fmt.Println(Nth([]string{"a", "b", "c", "d"}, 1)) fmt.Println(Nth([]string{"a", "b", "c", "d"}, -2)) fmt.Println(Nth([]string{"a", "b", "c", "d"}, 0)) fmt.Println(Nth([]string{"a", "b", "c", "d"}, -1))
Output: b c a d
func Partition ¶
Partition creates two slices, the first of which contains elements that `predicate` returns true for, with the second containing elements for which `predicate` returns false.
Example ¶
fmt.Println(Partition([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, func(n int) bool { return n%2 == 0 }))
Output: [2 4 6 8 10] [1 3 5 7 9]
func Pull ¶
func Pull[S ~[]T, T comparable](slice S, values ...T) S
Pull returns a new slice without all the given `values`.
Example ¶
array := []string{"a", "b", "c", "d", "e", "f", "g"} fmt.Println(array) fmt.Println(Pull(array, "a", "g")) fmt.Println(Pull(array, []string{"c"}...)) fmt.Println(Pull(array))
Output: [a b c d e f g] [b c d e f] [a b d e f g] [a b c d e f g]
func PullAll ¶
func PullAll[S ~[]T, T comparable](slice S, values []T) S
PullAll returns a new slice without the items in `values`.
Example ¶
array := []string{"a", "b", "c", "d", "e", "f", "g"} fmt.Println(array) fmt.Println(PullAll(array, []string{"a", "g"})) fmt.Println(PullAll(array, []string{"c"})) fmt.Println(PullAll(array, []string{}))
Output: [a b c d e f g] [b c d e f] [a b d e f g] [a b c d e f g]
func PullAllBy ¶
func PullAllBy[S ~[]T, T any, U comparable](slice S, values []T, iteratee func(T) U) S
PullAllBy returns a new slice without the items in `values`, with the comparison made by passing both values through the `iteratee` function.
Example ¶
fmt.Println(PullAllBy([]float64{1.2, 2.5, 3.14, 4.2}, []float64{3.8}, math.Floor))
Output: [1.2 2.5 4.2]
func PullAllWith ¶
PullAllWith returns a new slice without the items in `values`, with the comparison made using the given `comparator`.
Example ¶
fmt.Println(PullAllWith([]float64{1.2, 2.5, 3.14, 4.2}, []float64{3.8}, func(x, y float64) bool { return math.Floor(x) == math.Floor(y) }))
Output: [1.2 2.5 4.2]
func PullAt ¶
func PullAt[S ~[]T, T comparable](slice S, indexes ...int) S
PullAt returns a new slice without the items at the given indexes.
Example ¶
array := []string{"a", "b", "c", "d", "e", "f", "g"} fmt.Println(PullAt(array)) fmt.Println(PullAt(array, 0, 1)) fmt.Println(PullAt(array, []int{0, 2, 4, 6, 8, 10, 12}...))
Output: [a b c d e f g] [c d e f g] [b d f]
func Reduce ¶
func Reduce[S ~[]T, T any, U any](slice S, iteratee func(acc U, value T, index int, slice S) U, accumulator U) U
Reduce reduces `slice` to a value which is the accumulated result of running each element in `slice` through `iteratee`, where each successive invocation is supplied the return value of the previous one. `accumulator` is used as the initial value.
Example ¶
fmt.Println(Reduce([]float64{1.1, 2.2, 3.3, 4.4, 5.5}, func(acc float64, val float64, index int, arr []float64) float64 { return acc + val }, 0.0))
Output: 16.5
func ReduceRight ¶
func ReduceRight[S ~[]T, T any, U any](slice S, iteratee func(acc U, value T, index int, slice S) U, accumulator U) U
ReduceRight reduces `slice` to a value which is the accumulated result of running each element in `slice`, from right to left, through `iteratee`, where each successive invocation is supplied the return value of the previous one. `accumulator` is used as the initial value.
Example ¶
fmt.Println(ReduceRight([]string{"h", "e", "l", "l", "o"}, func(acc string, val string, index int, arr []string) string { return acc + val }, ""))
Output: olleh
func Reject ¶
Reject iterates over the elements of `slice`, returning a new slice of the elements for which `predicate` returns false.
Example ¶
fmt.Println(Reject([]int{1, 2, 3, 4, 5}, func(n int, index int, arr []int) bool { return n%2 == 0 }))
Output: [1 3 5]
func Remove ¶
Remove returns a new slice without the elements for which the `predicate` returns `true`.
Example ¶
array := []string{"a", "b", "c", "d", "e", "f", "g"} fmt.Println(array) fmt.Println(Remove(array, func(v string, i int, arr []string) bool { return i%2 == 0 }))
Output: [a b c d e f g] [b d f]
func Reverse ¶
func Reverse[S ~[]T, T any](slice S) S
Reverse return the reverse of `slice`: with the first element last, the second element second-to-last, and so on.
Example ¶
array := []int{1, 2, 3, 4, 5} fmt.Println(array) fmt.Println(Reverse(array))
Output: [1 2 3 4 5] [5 4 3 2 1]
func Some ¶
Some return true if the given `predicate` returns true for any element of the given slice.
Example ¶
fmt.Println(Some([]int{1, 2, 3}, func(v int, _ int, _ []int) bool { return v%2 == 0 })) fmt.Println(Some([]int{1, 3}, func(v int, _ int, _ []int) bool { return v%2 == 0 })) fmt.Println(Some([]int{}, func(v int, _ int, _ []int) bool { return v%2 == 0 }))
Output: true false false
func SortedIndex ¶
func SortedIndex[S ~[]T, T constraints.Ordered](slice S, value T) int
SortedIndex uses a binary search to determine the lowest index at which `value` should be inserted into `slice` in order to maintain its sort order.
Example ¶
fmt.Println(SortedIndex([]int{30, 50}, 40)) fmt.Println(SortedIndex([]int{5, 5}, 5))
Output: 1 0
func SortedIndexBy ¶
func SortedIndexBy[S ~[]T, T any, U constraints.Ordered](slice S, value T, iteratee func(T) U) int
SortedIndexBy uses a binary search to determine the lowest index at which `value` should be inserted into `slice` in order to maintain its sort order, with the `iteratee` function used to compute sort ranking.
Example ¶
fmt.Println(SortedIndexBy([]float64{1, -2, -4, 5, -6, 7}, 3, math.Abs)) fmt.Println(SortedIndexBy([]float64{1, -2, -4, 5, -5, 5, -6, 7}, 5, math.Abs))
Output: 2 3
func SortedIndexOf ¶
func SortedIndexOf[S ~[]T, T constraints.Ordered](slice S, value T) int
SortedIndexOf performs a binary search on a sorted `slice` to find the given `value`. Returns -1 if not found.
Example ¶
fmt.Println(SortedIndexOf([]int{4, 5, 5, 5, 6}, 5)) fmt.Println(SortedIndexOf([]int{4, 5, 5, 5, 6}, 0)) fmt.Println(SortedIndexOf([]int{4, 5, 5, 5, 6}, 42))
Output: 1 -1 -1
func SortedLastIndex ¶
func SortedLastIndex[S ~[]T, T constraints.Ordered](slice S, value T) int
SortedLastIndex returns the highest index at which `value` should be inserted into the sorted `slice` to maintain its sort order.
Example ¶
fmt.Println(SortedLastIndex([]int{4, 5, 5, 5, 6}, 5)) fmt.Println(SortedLastIndex([]int{4, 5, 5, 5, 6}, 42)) fmt.Println(SortedLastIndex([]int{4, 5, 5, 5, 6}, 0)) fmt.Println(SortedLastIndex([]int{4, 5, 5, 5}, 5))
Output: 4 5 0 4
func SortedLastIndexBy ¶
func SortedLastIndexBy[S ~[]T, T any, U constraints.Ordered](slice S, value T, iteratee func(T) U) int
SortedLastIndexBy returns the highest index at which `value` should be inserted into the sorted `slice` to maintain its sort order, with comparisons made on the result of passing all values through `iteratee`.
Example ¶
fmt.Println(SortedLastIndexBy([]float64{1, -2, -4, 5, -5, 5, -5, -6, 7}, 5, math.Abs)) fmt.Println(SortedLastIndexBy([]float64{1, -2, -4, 5, -5, 5, -5}, 5, math.Abs))
Output: 7 7
func SortedLastIndexOf ¶
func SortedLastIndexOf[S ~[]T, T constraints.Ordered](slice S, value T) int
SortedLastIndexOf returns the highest index at which the `value` is present in the sorted `slice`.
Example ¶
fmt.Println(SortedLastIndexOf([]int{4, 5, 5, 5, 6}, 5)) fmt.Println(SortedLastIndexOf([]int{4, 5, 5, 5, 6}, 42)) fmt.Println(SortedLastIndexOf([]int{5}, 5)) fmt.Println(SortedLastIndexOf([]int{}, 42))
Output: 3 -1 0 -1
func Take ¶
Take returns a new slice with `n` elements taken from the beginning.
Example ¶
fmt.Println(Take([]int{1, 2, 3}, 1)) fmt.Println(Take([]int{1, 2, 3}, 2)) fmt.Println(Take([]int{1, 2, 3}, 5)) fmt.Println(Take([]int{1, 2, 3}, 0))
Output: [1] [1 2] [1 2 3] []
func TakeRight ¶
TakeRight returns a new slice with `n` elements taken from the end.
Example ¶
fmt.Println(TakeRight([]int{1, 2, 3}, 1)) fmt.Println(TakeRight([]int{1, 2, 3}, 2)) fmt.Println(TakeRight([]int{1, 2, 3}, 5)) fmt.Println(TakeRight([]int{1, 2, 3}, 0))
Output: [3] [2 3] [1 2 3] []
func TakeRightWhile ¶
TakeRightWhile creates a slice of elements taken from the end of `slice`. Elements are taken until the `predicate` returns false.
Example ¶
haystack := []string{"h1", "h2", "h3", "needle", "h4", "h5", "needle", "h6", "h7", "h8"} fmt.Println(TakeRightWhile(haystack, func(item string, index int, arr []string) bool { return item != "needle" })) fmt.Println(TakeRightWhile(haystack, func(item string, index int, arr []string) bool { return item != "42" })) fmt.Println(TakeRightWhile([]string{}, func(item string, index int, arr []string) bool { return item != "needle" }))
Output: [h6 h7 h8] [h1 h2 h3 needle h4 h5 needle h6 h7 h8] []
func TakeWhile ¶
TakeWhile creates a slice of elements taken from the beginning of `slice`. Elements are taken until the `predicate` returns false.
Example ¶
haystack := []string{"h1", "h2", "h3", "needle", "h4", "h5", "needle", "h6", "h7", "h8"} fmt.Println(TakeWhile(haystack, func(item string, index int, arr []string) bool { return item != "needle" })) fmt.Println(TakeWhile(haystack, func(item string, index int, arr []string) bool { return item != "42" })) fmt.Println(TakeWhile([]string{}, func(item string, index int, arr []string) bool { return item != "needle" }))
Output: [h1 h2 h3] [h1 h2 h3 needle h4 h5 needle h6 h7 h8] []
func Union ¶
func Union[S ~[]T, T comparable](slices ...S) S
Union creates a new slice, in order, of unique values of all the given slices. Uses `==` for equality checks.
Example ¶
fmt.Println(Union([]int{2}, []int{1, 2})) fmt.Println(Union([]int{2}, []int{1, 2}, []int{}, []int{2, 4, 6}))
Output: [2 1] [2 1 4 6]
func UnionBy ¶
func UnionBy[S ~[]T, T any, U comparable](iteratee func(T) U, slices ...S) S
UnionBy creates a new slice, in order, of unique values of all the given slices. Uses the result of the given `iteratee` to check equality.
Example ¶
fmt.Println(UnionBy(math.Floor, []float64{2.3}, []float64{1.2, 2.7}))
Output: [2.3 1.2]
func UnionWith ¶
UnionWith creates a new slice, in order, of unique values of all the given slices. Uses the given `comparator` to check equality between elements.
Example ¶
fmt.Println(UnionWith(func(a, b float64) bool { return math.Floor(a) == math.Floor(b) }, []float64{2.3}, []float64{1.2, 2.7}))
Output: [2.3 1.2]
func Uniq ¶
func Uniq[S ~[]T, T comparable](slice S) S
Uniq returns a new slice, in order, with no duplicates, with only the first occurrence of each element kept. Comparison is performed with `==`.
Example ¶
fmt.Println(Uniq([]int{2, 1, 2}))
Output: [2 1]
func UniqBy ¶
func UniqBy[S ~[]T, T any, U comparable](iteratee func(T) U, slice S) S
UniqBy returns a new slice, in order, with no duplicates, with only the first occurrence of each element kept. Comparison is performed with `==` on the result of passing each element through the given `iteratee`.
Example ¶
fmt.Println(UniqBy(math.Floor, []float64{2.3, 1.5, 2.6}))
Output: [2.3 1.5]
func UniqWith ¶
UniqWith returns a new slice, in order, with no duplicates, with only the first occurrence of each element kept. Comparison is performed using the given `comparator`.
Example ¶
fmt.Println(UniqWith(func(a, b float64) bool { return math.Floor(a) == math.Floor(b) }, []float64{2.3, 1.5, 2.6}))
Output: [2.3 1.5]
func Without ¶
func Without[S ~[]T, T comparable](slice S, values ...T) S
Without returns a new slice without the given elements. Uses `==` for equality checks.
Example ¶
fmt.Println(Without([]int{2, 1, 2, 3}, 1, 2)) fmt.Println(Without([]int{}))
Output: [3] []
func Xor ¶
func Xor[S ~[]T, T comparable](slices ...S) S
Xor returns a new slice of unique values that is the symmetric difference (elements which are any of the sets but not in their intersection) of the given slices. The order of result values is determined by the order they occur in the slices.
Example ¶
fmt.Println(Xor([]int{2, 1}, []int{2, 3}))
Output: [1 3]
func XorBy ¶
func XorBy[S ~[]T, T any, U comparable](iteratee func(T) U, slices ...S) S
XorBy returns a new slice of unique values that is the symmetric difference (elements which are any of the sets but not in their intersection) of the given slices. The order of result values is determined by the order they occur in the slices. Equality is determined by passing elements through the given `iteratee`.
Example ¶
fmt.Println(XorBy(math.Floor, []float64{2.1, 1.2}, []float64{2.3, 3.4}))
Output: [1.2 3.4]
func XorWith ¶
XorWith returns a new slice of unique values that is the symmetric difference (elements which are any of the sets but not in their intersection) of the given slices. The order of result values is determined by the order they occur in the slices. Equality is determined by passing elements to the given `comparator`.
Example ¶
fmt.Println(XorWith(func(a, b float64) bool { return math.Floor(a) == math.Floor(b) }, []float64{2.1, 1.2}, []float64{2.3, 3.4}))
Output: [1.2 3.4]
Types ¶
This section is empty.