sliceops

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter added in v0.4.0

func Filter[E any](sl []E, fn func(E) bool) []E

Filter returns a new slice with only the items that the given function returns true for.

func Index added in v0.4.0

func Index[E comparable](sl []E, v E) int

Index returns the first (lowest) index of the value within the given slice. If the value is not found, -1 is returned. slices is only in stdlib as of Go 1.21; need to have this for 1.20

func Insert

func Insert[E any](sl []E, index int, item E) ([]E, error)

Insert inserts the given item at the given index. If the index is larger than possible or -1, the item is inserted at the end of the list via append. All items at list.Items[index] are moved up by one to make room for the new item.

func Move

func Move[E any](sl []E, from, to int) ([]E, error)

Move moves the item at index from to index to. If from is out of bounds, an error is returned. If to is negative or greater than allowed index, the item is moved to the end of the list.

func RealIndex

func RealIndex[E any](sl []E, idx int, clampMax bool) (int, error)

RealIndex returns the given index if it is valid. If the index is -1, it is converted to the highest valid index. If the index is greater than the highest valid, the behavior depends on clampMax; if clampMax is true, the index is converted to the highest valid index, and if clampMax is false, an error is returned. If a valid index is not possible, an error is returned. If the returned error is nil, the returned index is guaranteed to be able to be used in sl without panicking in its current state. Not thread-safe.

Even if an error is returned, the index at its last value is also returned, which informs what the actual index was at the time the error was discoverd.

func Remove

func Remove[E any](sl []E, index int) ([]E, error)

Remove removes the item at the given index. If the index does not exist, an error is returned. -1 denotes the element at the end of the list.

Types

This section is empty.

Jump to

Keyboard shortcuts

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