slices

package
v1.2.31 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 2 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And added in v1.2.13

func And[E comparable](s ...E) bool

And tests whether the slice satisfying c != zero within all c in the slice. return true if len(s) == 0

func AndFunc added in v1.2.13

func AndFunc[S ~[]E, E any](s S, f func(E) bool) bool

AndFunc tests the slice satisfying f(c), or true if none do. return true if len(s) == 0

func Filter added in v1.2.16

func Filter[S ~[]E, E comparable](s S) S

Filter returns a slice satisfying c != zero within all c in the slice.

func FilterFunc added in v1.2.16

func FilterFunc[S ~[]E, E any](s S, f func(E) bool) S

FilterFunc returns a slice satisfying f(c) within all c in the slice.

func FirstFunc added in v1.2.11

func FirstFunc[S ~[]E, E any](s S, f func(E) bool) (e E, ok bool)

FirstFunc returns the first item from the slice satisfying f(c), or zero if none do.

func FirstOrZero added in v1.2.11

func FirstOrZero[E comparable](s ...E) E

FirstOrZero returns the first non-zero item from the slice, or zero if none do.

func FirstOrZeroFunc added in v1.2.11

func FirstOrZeroFunc[S ~[]E, E any](s S, f func(E) bool) E

FirstOrZeroFunc returns the first item from the slice satisfying f(c), or zero if none do.

func LinearSearch

func LinearSearch[E constraints.Ordered](x []E, target E) (int, bool)

LinearSearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order. Note: Binary-search was compared using the benchmarks. The following code is equivalent to the linear search above:

pos := sort.Search(len(x), func(i int) bool {
    return target < x[i]
})

The binary search wins for very large boundary sets, but the linear search performs better up through arrays between 256 and 512 elements, so we continue to prefer linear search.

func LinearSearchFunc

func LinearSearchFunc[E any](x []E, target E, cmp func(E, E) int) (int, bool)

LinearSearchFunc works like LinearSearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func Or added in v1.2.13

func Or[E comparable](s ...E) bool

Or tests whether the slice satisfying c != zero within any c in the slice. return true if len(s) == 0

func OrFunc added in v1.2.13

func OrFunc[S ~[]E, E any](s S, f func(E) bool) bool

OrFunc tests the slice satisfying f(c), or false if none do. return true if len(s) == 0

func Reverse added in v1.2.7

func Reverse[S ~[]E, E any](x S)

Reverse reorder a slice of any ordered type in reverse order.

func Split added in v1.2.31

func Split[S ~[]E, E any](s S, sep int) []S

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If sep is <= zero, Split splits after each element, as chunk size is 1. If both s and sep are empty or zero, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

func SplitN

func SplitN[S ~[]E, E any](s S, sep int, n int) []S

SplitN slices s into subslices and returns a slice of the subslices.

The count determines the number of subslices to return:

  n > 0: at most n subslices; the last subslices will be the unsplit remainder.
		The count determines the number of subslices to return:
  		sep > 0: Split splits every sep as chunk size; the last subslices will be the unsplit remainder.
  		sep <= 0: take len(S)/n as chunk size
  n == 0: the result is nil (zero subslices)
  n < 0: all subslices as n == len(s)

Edge cases for s and sep (for example, zero) are handled as described in the documentation for Split.

Types

This section is empty.

Jump to

Keyboard shortcuts

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