Documentation ¶
Overview ¶
Package collection provides utility functions for slices.
Index ¶
- func BinarySearch[T any](list []T, less func(T) bool) int
- func CommonPrefix[T comparable](a, b []T) []T
- func Copy[T any](val []T) []T
- func Equal[T comparable](a, b []T) bool
- func Find[T any](list []T, predicate func(T) bool) T
- func FindIndex[T any](list []T, predicate func(T) bool) int
- func Insert[T any](list []T, index int, val T) []T
- func Reverse[T any](value []T) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinarySearch ¶
BinarySearch returns the index where the target should exist based on the less function provided. less function should return true when value is less.
Example ¶
res1 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 4 }) res2 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 6 }) res3 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 2 }) fmt.Println(res1, res2, res3)
Output: 2 3 0
func CommonPrefix ¶
func CommonPrefix[T comparable](a, b []T) []T
CommonPrefix returns a new slice with common elements at the beginning of the both inputs.
func Copy ¶
func Copy[T any](val []T) []T
Copy returns a new splice of the input using standard "copy" function.
func Equal ¶
func Equal[T comparable](a, b []T) bool
Equal returns true if input a and b have the same value in the same order.
func Find ¶
Find returns first value of slice when predicate returns true. It returns default value of T if no match is found.
func FindIndex ¶
FindIndex returns first index when predicate returns true. It returns "-1" if no match is found.
Types ¶
This section is empty.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package bytes provides utility functions for byte slices.
|
Package bytes provides utility functions for byte slices. |
Package ints provides utility functions for int slices.
|
Package ints provides utility functions for int slices. |
Package strings provides utility functions for string slices.
|
Package strings provides utility functions for string slices. |