Documentation ¶
Overview ¶
Package slices provides a set of mostly generic functions for working with slices.
Index ¶
- func All[E any](source []E, predicate func(E) bool) bool
- func AllIndexed[E any](source []E, predicate func(E, int) bool) bool
- func Any[E any](source []E, predicate func(E) bool) bool
- func AnyIndexed[E any](source []E, predicate func(E, int) bool) bool
- func BinarySearch[E constraints.Ordered](source []E, element E) (int, bool)
- func BinarySearchFunc[E any](source []E, compare func(E) int) (int, bool)
- func BubbleSort[E constraints.Ordered](source []E) []E
- func BubbleSortFunc[E any](source []E, compare func(E, E) int) []E
- func BubbleSortInPlace[E constraints.Ordered](source []E)
- func BubbleSortInPlaceFunc[E any](source []E, compare func(E, E) int)
- func Clone[E any](source []E) []E
- func Compact[E comparable](source []E) []E
- func CompactFunc[E any](source []E, equals func(E, E) bool) []E
- func Compare[E constraints.Ordered](s1, s2 []E) int
- func CompareFunc[E any](a []E, b []E, compare func(E, E) int) int
- func Contains[E comparable](source []E, item E) bool
- func ContainsFunc[E any](source []E, predicate func(E) bool) bool
- func ContainsFuncIndexed[E any](source []E, predicate func(E, int) bool) bool
- func Count[E comparable](source []E, item E) int
- func CountFunc[E any](source []E, predicate func(E) bool) int
- func CountIndexedFunc[E any](source []E, predicate func(E, int) bool) int
- func Delete[E any](source []E, start int, count int) []E
- func Equals[E comparable](a []E, b []E) bool
- func EqualsFunc[E any](a []E, b []E, equals func(E, E) bool) bool
- func ExtractRange[E any](source []E, start int, count int) []E
- func ExtractRangeStep[E any](source []E, start int, count int, step int) []E
- func Filter[E any](source []E, predicate func(E) bool) []E
- func FilterIndexed[E any](source []E, predicate func(E, int) bool) []E
- func Flatten[E any](source [][]E) []E
- func FlattenMap[E, R any](source []E, mapper func(E) []R) []R
- func GroupBy[E any, K comparable](source []E, keySelector func(E) K) map[K][]E
- func GroupByIndexed[E any, K comparable](source []E, keySelector func(E, int) K) map[K][]E
- func Grow[E any](source []E, amount int) []E
- func Index[E comparable](source []E, item E) int
- func IndexFunc[E any](source []E, predicate func(E) bool) int
- func IndexIndexedFunc[E any](source []E, predicate func(E, int) bool) int
- func Indices[E comparable](source []E, item E) []int
- func IndicesFunc[E any](source []E, predicate func(E) bool) []int
- func IndicesIndexedFunc[E any](source []E, predicate func(E, int) bool) []int
- func Insert[E any](destination []E, start int, source []E) []E
- func IsSorted[E constraints.Ordered](source []E) bool
- func IsSortedFunc[E any](source []E, compare func(E, E) int) bool
- func LastIndex[E comparable](source []E, item E) int
- func LastIndexFunc[E any](source []E, predicate func(E) bool) int
- func LastIndexIndexedFunc[E any](source []E, predicate func(E, int) bool) int
- func Map[E, R any](source []E, mapper func(E) R) []R
- func MapIndexed[E, R any](source []E, mapper func(E, int) R) []R
- func MapRange[R any](start, count int, mapper func(int) R) []R
- func MapRangeStep[R any](start, count, step int, mapper func(int) R) []R
- func MergeSort[E constraints.Ordered](source []E) []E
- func MergeSortFunc[E any](source []E, compare func(E, E) int) []E
- func MergeSorted[E constraints.Ordered](left []E, right []E) []E
- func MergeSortedFunc[E any](left []E, right []E, compare func(E, E) int) []E
- func Partition[E any](source []E, predicate func(E) bool) (trueValues []E, falseValues []E)
- func QuickSort[E constraints.Ordered](source []E) []E
- func QuickSortFunc[E any](source []E, compare func(E, E) int) []E
- func QuickSortInPlace[E constraints.Ordered](source []E)
- func QuickSortInPlaceFunc[E any](source []E, compare func(E, E) int)
- func Range(start, count int) []int
- func RangeStep(start, count, step int) []int
- func Reduce[E, R any](source []E, seed R, reducer func(R, E) R) R
- func ReduceIndexed[E, R any](source []E, seed R, reducer func(R, E, int) R) R
- func ReduceRight[E, R any](source []E, seed R, reducer func(R, E) R) R
- func ReduceRightIndexed[E, R any](source []E, seed R, reducer func(R, E, int) R) R
- func Replace[E any](source []E, start int, count int, replacement []E) []E
- func Reverse[E any](source []E) []E
- func ReverseInPlace[E any](source []E)
- func Trim[E comparable](source []E, element E) []E
- func TrimCapacity[E any](source []E) []E
- func TrimEnd[E comparable](source []E, element E) []E
- func TrimEndFunc[E any](source []E, predicate func(E) bool) []E
- func TrimEndIndexedFunc[E any](source []E, predicate func(E, int) bool) []E
- func TrimFunc[E any](source []E, predicate func(E) bool) []E
- func TrimIndexedFunc[E any](source []E, predicate func(E, int) bool) []E
- func TrimStart[E comparable](source []E, element E) []E
- func TrimStartFunc[E any](source []E, predicate func(E) bool) []E
- func TrimStartIndexedFunc[E any](source []E, predicate func(E, int) bool) []E
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all items in the source slice match the predicate. If the source slice is nil or empty, true is returned.
func AllIndexed ¶
AllIndexed returns true if all items in the source slice match the predicate.
func Any ¶
Any returns true if any item in the source slice matches the predicate. If the source slice is nil or empty, false is returned.
func AnyIndexed ¶
AnyIndexed returns true if any item in the source slice matches the predicate.
func BinarySearch ¶
func BinarySearch[E constraints.Ordered](source []E, element E) (int, bool)
BinarySearch returns the index where an element is found, or would be found, in the given slice. The slice must be sorted. The function returns a second item which is true if the element was found.
func BinarySearchFunc ¶
BinarySearchFunc returns the index where an element is found, or would be found, in the given slice. The slice must be sorted. The given predicate must return 0 if the element is found, -1 if the element is less than the current element, and 1 if the element is greater than the current element. The function returns a second item which is true if the element was found.
func BubbleSort ¶
func BubbleSort[E constraints.Ordered](source []E) []E
BubbleSort is a function that sorts a slice using the bubble sort algorithm.
func BubbleSortFunc ¶
BubbleSortFunc is a function that sorts a slice using the bubble sort algorithm.
func BubbleSortInPlace ¶
func BubbleSortInPlace[E constraints.Ordered](source []E)
BubbleSortInPlace is a function that sorts a slice using the bubble sort algorithm.
func BubbleSortInPlaceFunc ¶
BubbleSortInPlaceFunc is a function that sorts a slice using the bubble sort algorithm.
func Compact ¶
func Compact[E comparable](source []E) []E
Compact returns a new slice with all duplicate adjacent items removed.
func CompactFunc ¶
CompactFunc returns a new slice with all duplicate adjacent items removed. The predicate is used to determine if two items are the same or equals.
func Compare ¶
func Compare[E constraints.Ordered](s1, s2 []E) int
Compare returns 1 if the first slice is greater than the second, -1 if the first slice is less than the second, and 0 if the slices are equal.
func CompareFunc ¶
CompareFunc returns 1 if the first slice is greater than the second, -1 if the first slice is less than the second, and 0 if the slices are equal, by the given comparison function
func Contains ¶
func Contains[E comparable](source []E, item E) bool
Contains returns true if the source slice contains the specified item.
func ContainsFunc ¶
ContainsFunc returns true if the source slice contains an item that matches the specified predicate.
func ContainsFuncIndexed ¶
ContainsFuncIndexed returns true if the source slice contains an item that matches the specified predicate.
func Count ¶
func Count[E comparable](source []E, item E) int
Count returns the number of items in the source slice that are equal to the specified item.
func CountFunc ¶
CountFunc returns the number of items in the source slice that match the specified predicate.
func CountIndexedFunc ¶
CountIndexedFunc returns the number of items in the source slice that match the specified predicate.
func Delete ¶
Delete deletes the elements of source, starting at start, with count elements. It is equivalent to replacing count elements of source with 0 elements, starting at start.
func Equals ¶
func Equals[E comparable](a []E, b []E) bool
Equals returns true if the two slices are equal.
func EqualsFunc ¶
EqualsFunc returns true if the two slices are equal by the given predicate.
func ExtractRange ¶
ExtractRange returns a slice of elements from source, starting at start, with count elements.
func ExtractRangeStep ¶
ExtractRangeStep returns a slice of elements from source, starting at start, with count elements, and with a step of step.
func Filter ¶
Filter returns a new slice containing only the items from the source slice that match the specified predicate.
func FilterIndexed ¶
FilterIndexed returns a new slice containing only the items from the source slice that match the specified predicate. The function is invoked with the index of the element as the second argument.
func Flatten ¶
func Flatten[E any](source [][]E) []E
Flatten returns a new slice containing all the items from the source slice of slices.
func FlattenMap ¶
func FlattenMap[E, R any](source []E, mapper func(E) []R) []R
func GroupBy ¶
func GroupBy[E any, K comparable](source []E, keySelector func(E) K) map[K][]E
GroupBy returns a map of slices, where each slice contains all the items from the source slice where the specified key selector returns the same key.
func GroupByIndexed ¶
func GroupByIndexed[E any, K comparable](source []E, keySelector func(E, int) K) map[K][]E
GroupByIndexed returns a map of slices, where each slice contains all the items from the source slice where the specified key selector returns the same key.
func Index ¶
func Index[E comparable](source []E, item E) int
Index returns the index of the first element in source that is equal to the given item. If the item is not found, -1 is returned.
func IndexFunc ¶
IndexFunc returns the index of the first element in source that satisfies the given predicate. If the item is not found, -1 is returned.
func IndexIndexedFunc ¶
IndexIndexedFunc returns the index of the first element in source that satisfies the given predicate. If the item is not found, -1 is returned.
func Indices ¶
func Indices[E comparable](source []E, item E) []int
Indices returns the indices of all elements in source that are equal to the given item.
func IndicesFunc ¶
IndicesFunc returns the indices of all elements in source that satisfy the given predicate.
func IndicesIndexedFunc ¶
IndicesIndexedFunc returns the indices of all elements in source that satisfy the given predicate. The function is invoked with the index of the element as the second argument.
func Insert ¶
Insert inserts the elements of source into the destination slice at the specified start. It is equivalent to replacing 0 elements of the destination slice with the elements of source, starting at start.
func IsSorted ¶
func IsSorted[E constraints.Ordered](source []E) bool
IsSorted returns true if the slice is sorted.
func IsSortedFunc ¶
IsSortedFunc returns true if the slice is sorted by the given predicate.
func LastIndex ¶
func LastIndex[E comparable](source []E, item E) int
LastIndex returns the index of the first element in source that is equal to the given item. If the item is not found, -1 is returned.
func LastIndexFunc ¶
LastIndexFunc returns the index of the first element in source that satisfies the given predicate. If the item is not found, -1 is returned.
func LastIndexIndexedFunc ¶
LastIndexIndexedFunc returns the index of the first element in source that satisfies the given predicate. The function is invoked with the index of the element as the second argument. If the item is not found, -1 is returned.
func Map ¶
func Map[E, R any](source []E, mapper func(E) R) []R
Map returns a new slice containing the results of applying the given function to each element of the source slice.
func MapIndexed ¶
MapIndexed returns a new slice containing the results of applying the given function to each element of the source slice. The function is invoked with the index of the element as the second argument.
func MapRange ¶
MapRange returns a new slice containing the results of applying the given function to each element of the range.
func MapRangeStep ¶
MapRangeStep returns a new slice containing the results of applying the given function to each element of the range.
func MergeSort ¶
func MergeSort[E constraints.Ordered](source []E) []E
MergeSort is a function that sorts a slice using the merge sort algorithm.
func MergeSortFunc ¶
MergeSortFunc is a function that sorts a slice using the merge sort algorithm.
func MergeSorted ¶
func MergeSorted[E constraints.Ordered](left []E, right []E) []E
MergeSorted is a function that merges two sorted slices into a single sorted slice.
func MergeSortedFunc ¶
MergeSortedFunc is a function that merges two sorted slices into a single sorted slice.
func Partition ¶
Partition returns two slices, the first containing the elements for which the predicate returns true, the second containing the elements for which the predicate returns false.
func QuickSort ¶
func QuickSort[E constraints.Ordered](source []E) []E
QuickSort sorts the given slice.
func QuickSortFunc ¶
QuickSortFunc sorts the given slice using the given comparison function.
func QuickSortInPlace ¶ added in v0.0.5
func QuickSortInPlace[E constraints.Ordered](source []E)
QuickSortInPlace sorts the given slice in place.
func QuickSortInPlaceFunc ¶ added in v0.0.5
QuickSortInPlaceFunc sorts the given slice in place using the given comparison function.
func Range ¶
Range returns a new slice containing the range of integers from start to start+count-1.
func RangeStep ¶
RangeStep returns a new slice containing the range of integers from start to start+step*count-1 with the given step.
func Reduce ¶
func Reduce[E, R any](source []E, seed R, reducer func(R, E) R) R
Reduce returns the result of applying the reducer function to each element of the source, passing the result of the previous application to the next application.
func ReduceIndexed ¶
ReduceIndexed returns the result of applying the reducer function to each element of the source, passing the result of the previous application to the next application. The function is invoked with the index of the element as the third argument.
func ReduceRight ¶
func ReduceRight[E, R any](source []E, seed R, reducer func(R, E) R) R
ReduceRight returns the result of applying the reducer function to each element of the source, passing the result of the previous application to the next application, from the end of the slice to the beginning.
func ReduceRightIndexed ¶
ReduceRightIndexed returns the result of applying the reducer function to each element of the source, passing the result of the previous application to the next application, from the end of the slice to the beginning. The function is invoked with the index of the element as the third argument.
func Replace ¶
Replace replaces the elements of source with the elements of replacement, starting at start, with count elements. It is equivalent to first deleting count elements from source, starting at start, and then inserting replacement at start.
func Reverse ¶
func Reverse[E any](source []E) []E
Reverse returns a new slice with the elements of the source slice in reverse order.
func ReverseInPlace ¶
func ReverseInPlace[E any](source []E)
ReverseInPlace reverses the elements of the source slice in place.
func Trim ¶
func Trim[E comparable](source []E, element E) []E
Trim returns a slice with all leading and trailing elements that are equal to the specified element removed.
func TrimCapacity ¶
func TrimCapacity[E any](source []E) []E
TrimCapacity returns a slice with all extra capacity removed.
func TrimEnd ¶
func TrimEnd[E comparable](source []E, element E) []E
TrimEnd returns a slice with all trailing elements that are equal to the specified element removed.
func TrimEndFunc ¶
TrimEndFunc returns a slice with all trailing elements that satisfy the predicate removed.
func TrimEndIndexedFunc ¶
TrimEndIndexedFunc returns a slice with all trailing elements that satisfy the predicate removed. The function is invoked with the index of the element as the second argument.
func TrimFunc ¶
TrimFunc returns a slice with all leading and trailing elements that satisfy the predicate removed.
func TrimIndexedFunc ¶
TrimIndexedFunc returns a slice with all leading and trailing elements that satisfy the predicate removed. The function is invoked with the index of the element as the second argument.
func TrimStart ¶
func TrimStart[E comparable](source []E, element E) []E
TrimStart returns a slice with all leading elements that are equal to the specified element removed.
func TrimStartFunc ¶
TrimStartFunc returns a slice with all leading elements that satisfy the predicate removed.
func TrimStartIndexedFunc ¶
TrimStartIndexedFunc returns a slice with all leading elements that satisfy the predicate removed. The function is invoked with the index of the element as the second argument.
Types ¶
This section is empty.
Source Files ¶
- All.go
- Any.go
- BinarySearch.go
- BubbleSort.go
- Clone.go
- Compact.go
- Compare.go
- Contains.go
- Count.go
- Delete.go
- Equals.go
- ExtractRange.go
- Filter.go
- Flatten.go
- FlattenMap.go
- GroupBy.go
- Grow.go
- Index.go
- Indices.go
- Insert.go
- IsSorted.go
- LastIndex.go
- Map.go
- MapRange.go
- MergeSort.go
- Partition.go
- QuickSort.go
- Range.go
- Reduce.go
- Replace.go
- Reverse.go
- Trim.go
- TrimCapacity.go
- slices.go