slices

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 2 Imported by: 0

README

Slices

Functions

  • All
  • AllIndexed
  • Any
  • AnyIndexed
  • BinarySearch
  • BinarySearchFunc
  • BubbleSort
  • BubbleSortFunc
  • Clone
  • Compact
  • CompactFunc
  • Compare
  • CompareFunc
  • Contains
  • ContainsFunc
  • ContainsIndexedFunc
  • Count
  • CountFunc
  • CountIndexedFunc
  • Delete
  • Equals
  • EqualsFunc
  • ExtractRange
  • ExtractRangeStep
  • Filter
  • FilterIndexed
  • Flatten
  • FlattenMap
  • GroupBy
  • GroupByIndexed
  • Grow
  • Index
  • IndexFunc
  • IndexIndexedFunc
  • Indices
  • IndicesFunc
  • IndicesIndexedFunc
  • Insert
  • IsSorted
  • IsSortedFunc
  • LastIndex
  • LastIndexFunc
  • LastIndexIndexedFunc
  • Map
  • MapIndexed
  • MapRange
  • MapRangeStep
  • MergeSort
  • MergeSortFunc
  • MergeSorted
  • MergeSortedFunc
  • Partition
  • QuickSort
  • QuickSortFunc
  • Range
  • RangeStep
  • Reduce
  • ReduceIndexed
  • ReduceRight
  • ReduceRightIndexed
  • Replace
  • Reverse
  • ReverseInPlace
  • Trim
  • TrimFunc
  • TrimStart
  • TrimStartFunc
  • TrimEnd
  • TrimEndFunc
  • TrimIndexed
  • TrimIndexedFunc
  • TrimStartIndexed
  • TrimStartIndexedFunc
  • TrimEndIndexed
  • TrimEndIndexedFunc
  • TrimCapacity

Documentation

Overview

Package slices provides a set of mostly generic functions for working with slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[E any](source []E, predicate func(E) bool) bool

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

func AllIndexed[E any](source []E, predicate func(E, int) bool) bool

AllIndexed returns true if all items in the source slice match the predicate.

func Any

func Any[E any](source []E, predicate func(E) bool) bool

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

func AnyIndexed[E any](source []E, predicate func(E, int) bool) bool

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

func BinarySearchFunc[E any](source []E, compare func(E) int) (int, bool)

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

func BubbleSortFunc[E any](source []E, compare func(E, E) int) []E

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

func BubbleSortInPlaceFunc[E any](source []E, compare func(E, E) int)

BubbleSortInPlaceFunc is a function that sorts a slice using the bubble sort algorithm.

func Clone

func Clone[E any](source []E) []E

Clone returns a copy of the source slice.

func Compact

func Compact[E comparable](source []E) []E

Compact returns a new slice with all duplicate adjacent items removed.

func CompactFunc

func CompactFunc[E any](source []E, equals func(E, E) bool) []E

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

func CompareFunc[E any](a []E, b []E, compare func(E, E) int) int

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

func ContainsFunc[E any](source []E, predicate func(E) bool) bool

ContainsFunc returns true if the source slice contains an item that matches the specified predicate.

func ContainsFuncIndexed

func ContainsFuncIndexed[E any](source []E, predicate func(E, int) bool) bool

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

func CountFunc[E any](source []E, predicate func(E) bool) int

CountFunc returns the number of items in the source slice that match the specified predicate.

func CountIndexedFunc

func CountIndexedFunc[E any](source []E, predicate func(E, int) bool) int

CountIndexedFunc returns the number of items in the source slice that match the specified predicate.

func Delete

func Delete[E any](source []E, start int, count int) []E

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

func EqualsFunc[E any](a []E, b []E, equals func(E, E) bool) bool

EqualsFunc returns true if the two slices are equal by the given predicate.

func ExtractRange

func ExtractRange[E any](source []E, start int, count int) []E

ExtractRange returns a slice of elements from source, starting at start, with count elements.

func ExtractRangeStep

func ExtractRangeStep[E any](source []E, start int, count int, step int) []E

ExtractRangeStep returns a slice of elements from source, starting at start, with count elements, and with a step of step.

func Filter

func Filter[E any](source []E, predicate func(E) bool) []E

Filter returns a new slice containing only the items from the source slice that match the specified predicate.

func FilterIndexed

func FilterIndexed[E any](source []E, predicate func(E, int) bool) []E

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 Grow

func Grow[E any](source []E, amount int) []E

Grow grows the slice by the specified amount.

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

func IndexFunc[E any](source []E, predicate func(E) bool) int

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

func IndexIndexedFunc[E any](source []E, predicate func(E, int) bool) int

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

func IndicesFunc[E any](source []E, predicate func(E) bool) []int

IndicesFunc returns the indices of all elements in source that satisfy the given predicate.

func IndicesIndexedFunc

func IndicesIndexedFunc[E any](source []E, predicate func(E, int) bool) []int

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

func Insert[E any](destination []E, start int, source []E) []E

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

func IsSortedFunc[E any](source []E, compare func(E, E) int) bool

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

func LastIndexFunc[E any](source []E, predicate func(E) bool) int

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

func LastIndexIndexedFunc[E any](source []E, predicate func(E, int) bool) int

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

func MapIndexed[E, R any](source []E, mapper func(E, int) R) []R

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

func MapRange[R any](start, count int, mapper func(int) R) []R

MapRange returns a new slice containing the results of applying the given function to each element of the range.

func MapRangeStep

func MapRangeStep[R any](start, count, step int, mapper func(int) R) []R

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

func MergeSortFunc[E any](source []E, compare func(E, E) int) []E

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

func MergeSortedFunc[E any](left []E, right []E, compare func(E, E) int) []E

MergeSortedFunc is a function that merges two sorted slices into a single sorted slice.

func Partition

func Partition[E any](source []E, predicate func(E) bool) (trueValues []E, falseValues []E)

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

func QuickSortFunc[E any](source []E, compare func(E, E) int) []E

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

func QuickSortInPlaceFunc[E any](source []E, compare func(E, E) int)

QuickSortInPlaceFunc sorts the given slice in place using the given comparison function.

func Range

func Range(start, count int) []int

Range returns a new slice containing the range of integers from start to start+count-1.

func RangeStep

func RangeStep(start, count, step int) []int

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

func ReduceIndexed[E, R any](source []E, seed R, reducer func(R, E, int) R) R

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

func ReduceRightIndexed[E, R any](source []E, seed R, reducer func(R, E, int) R) R

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

func Replace[E any](source []E, start int, count int, replacement []E) []E

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

func TrimEndFunc[E any](source []E, predicate func(E) bool) []E

TrimEndFunc returns a slice with all trailing elements that satisfy the predicate removed.

func TrimEndIndexedFunc

func TrimEndIndexedFunc[E any](source []E, predicate func(E, int) bool) []E

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

func TrimFunc[E any](source []E, predicate func(E) bool) []E

TrimFunc returns a slice with all leading and trailing elements that satisfy the predicate removed.

func TrimIndexedFunc

func TrimIndexedFunc[E any](source []E, predicate func(E, int) bool) []E

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

func TrimStartFunc[E any](source []E, predicate func(E) bool) []E

TrimStartFunc returns a slice with all leading elements that satisfy the predicate removed.

func TrimStartIndexedFunc

func TrimStartIndexedFunc[E any](source []E, predicate func(E, int) bool) []E

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL