slice

package module
v0.0.0-...-81c1f18 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 2 Imported by: 0

README

slice

Manipulate slices

GoDoc License

Package slice provides slice manipulation functions using generics. Functions that rely on comparing or ordering items in a slice operate on slices of any ordered type. Other slice manipulation functions work on any type.

Installation

$ go get github.com/gammazero/slice

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy[T any](s []T) []T

Copy makes a copy of a slice.

func Count

func Count[T constraints.Ordered](s []T, item T) int

Count counts the number of instances of item in s.

func Cut

func Cut[T constraints.Ordered](s []T, sep T) (before, after []T, found bool)

Cut slices s around the first instance of sep, returning the slice before and after sep. The found result reports whether sep is an element of s. If sep is not an element of s, cut returns s, nil, false.

func Delete

func Delete[T any](s []T, i int) []T

Delete removes the item at index i.

func DeleteFast

func DeleteFast[T any](s []T, i int) []T

DeleteFast removes the item at index i without preserving order.

func DeleteN

func DeleteN[T any](s []T, start, n int) []T

DeleteN removes items in s from index start to start + n. If n is -1, then s is truncated at start.

func Filter

func Filter[T any](s []T, keep func(T) bool) []T

Filter removes all items for which keep returns false.

func Index

func Index[T constraints.Ordered](s []T, item T) int

Index returns the index of the first instance of item in s, or -1 if item is not present in s.

func Insert

func Insert[T any](s []T, i int, items ...T) []T

Insert inserts items into s at the index i.

func LastIndex

func LastIndex[T constraints.Ordered](s []T, item T) int

LastIndex returns the index of the last instance of item in s, or -1 if item is not present in s.

func Pop

func Pop[T any](s []T) (T, []T)

Pop removes and returns the last item in the slice.

func Remove

func Remove[T constraints.Ordered](s []T, items ...T) []T

Remove returns a slize with all occurrances of each item removed.

func Replace

func Replace[T constraints.Ordered](s []T, old, new T, n int)

Replace replaces the first n instances of old, in s, with new. If n is -1, then there is no limit on the number of replacements.

func ReplaceLast

func ReplaceLast[T constraints.Ordered](s []T, old, new T, n int)

ReplaceLast replaces the last n instances of old, in s, with new. If n is -1, then there is no limit on the number of replacements.

func Reverse

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

Reverse reverses the order of items in s, in place.

func Sort

func Sort[T constraints.Ordered](s []T)

Sort sorts s in-place in ascending order.

func SortReverse

func SortReverse[T constraints.Ordered](s []T)

SortReverse sorts s in-place in descending order.

func Unique

func Unique[T constraints.Ordered](s []T) []T

Unique removes duplicate items in s, keeping only the first instance of each.

Types

This section is empty.

Jump to

Keyboard shortcuts

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