slices

package
v1.2.66 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 4 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 Contains added in v1.2.38

func Contains[E comparable](s []E, v E) bool

Contains reports whether v is present in s.

func ContainsFunc added in v1.2.38

func ContainsFunc[E any](s []E, f func(E) bool) bool

ContainsFunc reports whether v satisfying f(s[i]) is present in s.

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. Filter modifies the contents of the slice s; it does not create a new 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. FilterFunc modifies the contents of the slice s; it does not create a new 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 Group added in v1.2.65

func Group[S ~[]E, M map[E]N, E comparable, N int](s S) M

Group returns a map group by all elements in s that have the same values.

If s is nil, Group returns nil (zero map).

If s is empty, Group returns an empty map.

Else, Group returns a map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[E]N, E comparable, N constraints.Number] if go support template type deduction

func GroupFunc added in v1.2.65

func GroupFunc[S ~[]E, M map[K]S, E any, K comparable](s S, f func(E) K) M

GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values.

If s is nil, GroupFunc returns nil (zero map).

If s and f are both empty or nil, GroupFunc returns an empty map.

Else, GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[K]S, E any, K comparable] if go support template type deduction

func Intersect added in v1.2.38

func Intersect[S ~[]E, E comparable](s1, s2 S) S

Intersect returns a slice satisfying c != zero within all c in the slice. Intersect does not modify the contents of the slice s1 and s2; it creates a new slice.

func IntersectFunc added in v1.2.38

func IntersectFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S

IntersectFunc returns a slice satisfying f(c) within all c in the slice. IntersectFunc does not modify the contents of the slice s1 and s2; it creates a new slice.

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 Map added in v1.2.51

func Map[S ~[]E, E any, R []M, M string](s S) R

Map returns a slice mapped by format "%v" within all c in the slice. Map does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M ~string] if go support template type deduction

func MapFunc added in v1.2.51

func MapFunc[S ~[]E, E any, R []M, M any](s S, f func(E) M) R

MapFunc returns a slice mapped by f(c) within all c in the slice. MapFunc does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M any] if go support template type deduction

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. Reverse modifies the contents of the slice s; it does not create a new slice.

func Split added in v1.2.31

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

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

If s is less than sep and sep is more than zero, Split returns a slice of length 1 whose only element is s.

If s is nil, Split returns nil (zero subslices).

If both s and sep are empty or zero, Split returns an empty slice.

If sep is <= zero, Split splits after each element, as chunk size is 1.

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.

func TypeAssertFilter added in v1.2.58

func TypeAssertFilter[S ~[]E, M ~[]R, E any, R any](s S) M

TypeAssertFilter returns a slice satisfying r, ok := any(c).(R); ok == true within all r in the slice. TypeAssertFilter does not modify the contents of the slice s; it creates a new slice.

func TypeAssertFilterFunc added in v1.2.58

func TypeAssertFilterFunc[S ~[]E, M ~[]R, E any, R any](s S, f func(E) (R, bool)) M

TypeAssertFilterFunc returns a slice satisfying f(c) within all c in the slice. TypeAssertFilterFunc does not modify the contents of the slice s; it creates a new slice.

func Union added in v1.2.38

func Union[S ~[]E, E comparable](s1, s2 S) S

Union returns a slice satisfying c != zero within all c in the slice. Union replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Union does not modify the contents of the slice s1 and s2; it creates a new slice.

func UnionFunc added in v1.2.38

func UnionFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S

UnionFunc returns a slice satisfying f(c) within all c in the slice. UnionFunc does not modify the contents of the slice s1 and s2; it creates a new slice.

func Uniq added in v1.2.38

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

Uniq returns a slice satisfying c != zero within all c in the slice. Uniq modifies the contents of the slice s; it does not create a new slice.

func UniqFunc added in v1.2.38

func UniqFunc[S ~[]E, E any](s S, f func(v1, v2 E) bool) S

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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