slices

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: Apache-2.0 Imports: 0 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.2.0

func All[T any](s []T, f func(T) bool) bool

Report whether the given boolean function evaluates to true for all elements of the given slice. Returns true for nil or empty slices. All(s, f) is equivalent to !Any(s, !f) and None(s, !f).

func Any added in v0.2.0

func Any[T any](s []T, f func(T) bool) bool

Report whether the given boolean function evaluates to true for at least one element of the given slice. Returns false for nil or empty slices. Any(s, f) is equivalent to !All(s, !f) and !None(s, f).

func Collect added in v0.1.1

func Collect[S any, T any](s []S, f func(S) T) (r []T)

Collect (map) slice through given function. If the input is nil, it will return nil; if the input is empty, it will return an empty slice. Otherweise, it will return a slice of the same length as the input slice, containing the elements mapped through the provided function f.

func Contains

func Contains[T comparable](s []T, x T) bool

Check if slice contains given element.

func Equal

func Equal[T comparable](s []T, t []T) bool

Compare two slices of comparable elements. Slices with different length are never equal. Empty and nil slices are always equal (in particular, comparing an empty with a nil slice yields true). Otherwise, the two slices are equal, if all their elements are equal by means of the == operator.

func EqualBy

func EqualBy[S any, T any](s []S, t []T, f func(S, T) bool) bool

Compare two slices by a given equality function. Slices with different length are never equal. Empty and nil slices are always equal (in particular, comparing an empty with a nil slice yields true). Otherwise, the two slices are equal if all their elements are equal by means of the provided equality function.

func First added in v0.1.8

func First[T any](s []T, n uint) []T

Get first n elements of a slice. If n is greater than the length of the slice, the input slice will be returned. If the input is nil, it will return nil; otherwise, if the result is empty, it will return an empty slice.

func Last added in v0.1.8

func Last[T any](s []T, n uint) []T

Get last n elements of a slice. If n is greater than the length of the slice, the input slice will be returned. If the input is nil, it will return nil; otherwise, if the result is empty, it will return an empty slice.

func None added in v0.2.0

func None[T any](s []T, f func(T) bool) bool

Report whether the given boolean function evaluates to true for none of the elements of the given slice. Equivalently: evaluates to false for all of the elements of the given slice. Returns true for nil or empty slices. None(s, f) is equivalent to !Any(s, f) and All(s, !f).

func Remove

func Remove[T comparable](s []T, x T) (r []T)

Remove all occurrences of given element from slice (and return new slice; old slice remains unchanged). If the input is nil, it will return nil; otherwise, if the result is empty, it will return an empty slice.

func Reverse

func Reverse[T any](s []T) (r []T)

Reverse slice. If the input is nil, it will return nil; otherwise, if the input is empty, it will return an empty slice.

func Select added in v0.2.3

func Select[T any](s []T, f func(T) bool) (r []T)

Select slice by given function. If the input is nil, it will return nil; if the input is empty, it will return an empty slice. Otherweise, it will return a new slice containing those elements of the input slice, for which the provided function f evalutes to true.

func Sort

func Sort[T Orderable](s []T) []T

Sort slice of orderable elements. If the input is nil, it will return nil; otherwise, if the input is empty, it will return an empty slice.

func SortBy

func SortBy[T any](s []T, f func(x, y T) bool) (r []T)

Sort slice by given comparator function. If the input is nil, it will return nil; otherwise, if the input is empty, it will return an empty slice. The comparator function f(x,y) must return true if x is larger than y, and false if x is smaller than y; the return value in case of equality does not matter (may be true or false).

func Uniq

func Uniq[T comparable](s []T) (r []T)

Remove duplicates from slice of comparable elements. Preserves order. The first occurrence of an element will be kept, the other occurrences will be dropped. If the input is nil, it will return nil; otherwise, if the input is empty, it will return an empty slice.

func UniqBy added in v0.2.0

func UniqBy[S any, T comparable](s []S, f func(S) T) (r []S)

Remove duplicates from slice by given mapper function. Two elements are considered equal if the mapper function returns the same value for them. Preserves order. The first occurrence of an element will be kept, the other occurrences will be dropped. If the input is nil, it will return nil; otherwise, if the input is empty, it will return an empty slice.

Types

type Orderable

type Orderable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}

Orderable constraint.

Jump to

Keyboard shortcuts

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