slices

package
v0.0.0-...-703044a Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package slices defines various functions useful with slices of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[S ~[]E, E any](s S) S

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func Contains

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

Contains reports whether v is present in s.

func Delete

func Delete[S ~[]E, E any](s S, i int) S

Delete removes the element i from s, returning the modified slice.

func Dereference

func Dereference[E any](s []*E) []E

Dereference returns all non-nil references, dereferenced

func Equal

func Equal[E comparable](s1, s2 []E) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[E1, E2 comparable](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool

EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.

func Filter

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

Filter retains all elements in []E that f(E) returns true for. A new slice is created and returned. Use FilterInPlace to perform in-place

func FilterInPlace

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

FilterInPlace retains all elements in []E that f(E) returns true for. The array is *mutated in place* and returned. Used Filter to avoid mutation

func FindFunc

func FindFunc[E any](s []E, f func(E) bool) *E

FindFunc finds the first element matching the function, or nil if none do

func Map

func Map[E any, O any](s []E, f func(E) O) []O

Map runs f() over all elements in s and returns the result

func MapFilter

func MapFilter[E any, O any](s []E, f func(E) *O) []O

MapFilter runs f() over all elements in s and returns any non-nil results

func Reference

func Reference[E any](s []E) []*E

Reference takes a pointer to all elements in the slice

func Reverse

func Reverse[E any](r []E) []E

Reverse returns its argument array reversed

func Sort

func Sort[E constraints.Ordered](x []E) []E

Sort sorts a slice of any ordered type in ascending order. The slice is modified in place but returned.

func SortFunc

func SortFunc[S ~[]E, E any](x S, less func(a, b E) int) S

SortFunc sorts the slice x in ascending order as determined by the less function. This sort is not guaranteed to be stable. The slice is modified in place but returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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