slices

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

README

slices

Go Reference

Functions for working with Go slices.

Documentation

Overview

Package slices provides functions for working with Go slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adjust added in v0.6.0

func Adjust[T any](sl []T, length, capacity int) []T

Adjust adjusts length and capacity of the given slice. If capacity < length it is set to length. Panics if length < 0.

func All added in v0.2.0

func All[T any](sl []T, pred Predicate[T]) bool

All returns true if for all values in sl the function pred returns true. Returns true for an empty slice. If pred is nil, type T must be bool.

func Any added in v0.2.0

func Any[T any](sl []T, pred Predicate[T]) bool

Any returns true if for any value in sl the function pred returns true. Returns false for an empty slice. If pred is nil, type T must be bool.

func Clone

func Clone[T any](sl []T) []T

Clone clones the slice.

func Collect added in v1.0.0

func Collect[T any](seq iter.Seq[T]) []T

Collect returns a new slice with the values from seq.

func Concat

func Concat[T any](sls ...[]T) []T

Concat returns the concatenation of all given slices as a new slice.

func Contains

func Contains[T comparable](sl []T, v T) bool

Contains returns true if the value is in the slice.

func ContainsFunc

func ContainsFunc[T any](sl []T, v T, f CmpFunc[T]) bool

ContainsFunc returns true if the value is in the slice using a function to compare values.

func Create

func Create[T any](vs ...T) []T

Create returns a new slice with the given values.

func CreateFunc added in v0.5.0

func CreateFunc[T any](length int, fn func() T) []T

CreateFunc returns a slice of given length with values created by successive calls to fn.

func Delete added in v0.3.0

func Delete[T any](sl []T, idx ...int) []T

Delete deletes the values at the given indexes and returns the modified slice.

func DeletePred added in v0.3.0

func DeletePred[T any](sl []T, pred Predicate[T]) []T

DeletePred deletes the values for which pred returns true and returns the modified slice.

func DeleteRange added in v0.3.0

func DeleteRange[T any](sl []T, start, length int) []T

DeleteRange deletes length values beginning at start and returns the modified slice.

func DropWhile added in v0.3.0

func DropWhile[T any](sl []T, pred Predicate[T]) []T

DropWhile drops values from sl as long as pred returns true and returns the rest in a new slice.

func Equal

func Equal[T comparable](sl1, sl2 []T) bool

Equal returns true if the slices are equal.

func EqualFunc

func EqualFunc[T any](sl1, sl2 []T, f CmpFunc[T]) bool

EqualFunc returns true if the slices are equal using a function to compare values.

func Extrema added in v0.7.0

func Extrema[T Ordered](sl []T) (min, max T)

Extrema returns minimum and maximum from the slice. Panics if the slice is empty or nil.

func ExtremaFunc added in v0.7.0

func ExtremaFunc[T Ordered](sl []T, f LessFunc[T]) (min, max T)

ExtremaFunc returns minimum and maximum from the slice using a function to compare values. Panics if the slice is empty or nil.

func Fill added in v0.3.0

func Fill[T any](sl []T, v T)

Fill fills the slice with the given value including the unused capacity.

func Filter

func Filter[T any](sl []T, pred Predicate[T]) []T

Filter returns a new slice with all values for which pred returns true.

func FilterIndex added in v0.8.0

func FilterIndex[T any](sl []T, pred Predicate[int]) []T

FilterIndex returns a new slice with all values for which pred(index) returns true.

func Find

func Find[T comparable](sl []T, v T) int

Find returns the index of the value in sl or -1 if not found.

func FindAll added in v0.3.0

func FindAll[T comparable](sl []T, v T) []int

FindAll returns all indexes of the value v.

func FindAllFunc added in v0.3.0

func FindAllFunc[T any](sl []T, v T, f CmpFunc[T]) []int

FindAll returns all indexes of the value v using a function to compare values.

func FindFrom added in v0.3.0

func FindFrom[T comparable](sl []T, v T, start int) int

FindFrom returns the index of the value in sl or -1 if not found. It starts at index start.

func FindFromFunc added in v0.3.0

func FindFromFunc[T any](sl []T, v T, start int, f CmpFunc[T]) int

FindFromFunc returns the index of the value in sl or -1 if not found using a function to compare values. It starts at index start.

func FindFunc

func FindFunc[T any](sl []T, v T, f CmpFunc[T]) int

FindFunc returns the index of the value in sl or -1 if not found using a function to compare values.

func Flatten

func Flatten[T any](sl [][]T) []T

Flatten returns a new slice with one level of nesting removed from a slice of slices.

func Get added in v0.4.0

func Get[T any](sl []T, idx int) T

Get gets the element at index idx. If idx < 0 it computes the index from the end. Panics if idx >= len(sl) or the computed index is < 0.

func Group added in v0.3.0

func Group[T comparable](sl []T) [][]T

Group groups consecutive, equal values from sl.

func GroupFunc added in v0.3.0

func GroupFunc[T any](sl []T, f CmpFunc[T]) [][]T

GroupFunc groups consecutive, equal values from sl using a function to compare values.

func Insert added in v0.3.0

func Insert[T any](sl []T, idx int, v ...T) []T

Insert inserts values at index idx and returns the modified slice. If there is enough unused capacity, the original slice will be modified.

func IsSorted

func IsSorted[T Ordered](sl []T) bool

IsSorted returns true if the slice is sorted.

func IsSortedFunc

func IsSortedFunc[T any](sl []T, f LessFunc[T]) bool

IsSortedFunc returns true if the slice is sorted using a function to compare values.

func Iter added in v1.0.0

func Iter[T any](sl []T) iter.Seq2[int, T]

Iter returns an iterator over index-values pairs in the slice.

func Map

func Map[T, U any](sl []T, f MapFunc[T, U]) []U

Map maps values in a slice using a mapping function.

func Map2 added in v0.2.0

func Map2[T, U, V any](sl1 []T, sl2 []U, f Map2Func[T, U, V]) []V

Map2 maps values in two slices using a mapping function. It stops when the shortest slice is exhaustet.

func Map3 added in v0.2.0

func Map3[T, U, V, W any](sl1 []T, sl2 []U, sl3 []V, f Map3Func[T, U, V, W]) []W

Map3 maps values in three slices using a mapping function. It stops when the shortest slice is exhaustet.

func Max

func Max[T Ordered](sl []T) T

Max returns the maximum from the slice. Panics if the slice is empty or nil.

func MaxFunc

func MaxFunc[T any](sl []T, f LessFunc[T]) T

MaxFunc returns the maximum from the slice using a function to compare values. Panics if the slice is empty or nil.

func Min

func Min[T Ordered](sl []T) T

Min returns the minimum from the slice. Panics if the slice is empty or nil.

func MinFunc

func MinFunc[T any](sl []T, f LessFunc[T]) T

MinFunc returns the minimum from the slice using a function to compare values. Panics if the slice is empty or nil.

func Product

func Product[T Number](sl []T) T

Product returns the product of the values in a slice.

func Purge added in v0.3.0

func Purge[T comparable](sl, ps []T) []T

Purge returns a new slice with all values from sl which are not in ps.

func PurgeFunc added in v0.3.0

func PurgeFunc[T any](sl, ps []T, f CmpFunc[T]) []T

PurgeFunc returns a new slice with all values from sl which are not in ps using a function to compare values.

func RandomFloat

func RandomFloat(length int) []float64

RandomFloat returns a slice of given length with pseudo-random values in the interval [0.0,1.0).

func RandomInt

func RandomInt(length, n int) []int

RandomInt returns a slice of given length with pseudo-random values in the interval [0,n). n == 0 means no upper limit. Panics when n < 0

func Range

func Range[T Signed | Float](start, end, step T) []T

Range returns a slice with numbers in [start, end). Will panic if step is 0.

func Reduce added in v0.2.0

func Reduce[T, U any](sl []T, init U, f ReduceFunc[T, U]) U

Reduce reduces sl to a single value by accumulating the values in sl using function f. The first argument to f is the accumulated value (starting with init).

func Repeat

func Repeat[T any](v T, n int) []T

Repeat returns a slice with n repetitions of the given value. Pancis if n < 0.

func RepeatSlice added in v0.5.0

func RepeatSlice[T any](sl []T, n int) []T

Repeat returns a slice with n repetitions of the values in the given slice. Pancis if n < 0.

func Reverse

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

Reverse reverses the order of values in the slice.

func ReverseClone

func ReverseClone[T any](sl []T) []T

ReverseClone returns a clone of the slice with the order of values reversed.

func Search[T Ordered](sl []T, v T) (idx int, found bool)

Search does a binary search and returns idx and found. If found is true, idx is the first index at which v was found. If found is false, idx is the index at which v must be inserted to keep sl sorted. The slice sl must be sorted in ascending order.

func SearchFunc added in v0.2.0

func SearchFunc[T any](sl []T, v T, f LessFunc[T]) (int, bool)

SearchFunc does a binary search using a function to compare values. If found is true, idx is the first index at which v was found. If found is false, idx is the index at which v must be inserted to keep sl sorted. The slice sl must be sorted in ascending order according to function f.

func Shuffle

func Shuffle[T any](sl []T)

Shuffle pseudo-randomizes the order of the elements in the slice.

func Slice added in v0.4.0

func Slice[T any](sl []T, start, end int) []T

Slice returns a slice from sl. If start or end < 0 it computes the indexes from the end. Panics if the (computed) slice bounds are out of range.

func Sort

func Sort[T Ordered](sl []T)

Sort sorts the slice.

func SortClone

func SortClone[T Ordered](sl []T) []T

SortClone returns a sorted clone of the slice.

func SortCloneFunc

func SortCloneFunc[T any](sl []T, stable bool, f LessFunc[T]) []T

SortCloneFunc returns a sorted clone of the slice using a function to compare values.

func SortFunc

func SortFunc[T any](sl []T, stable bool, f LessFunc[T])

SortFunc sorts the slice using a function to compare values.

func Sum

func Sum[T Number](sl []T) T

Sum returns the sum of the values in a slice.

func TakeWhile added in v0.3.0

func TakeWhile[T any](sl []T, pred Predicate[T]) []T

TakeWhile takes values from sl as long as pred returns true and returns them in a new slice.

func Unique

func Unique[T comparable](sl []T) []T

Unique returns the unique values from a slice.

func UniqueFunc

func UniqueFunc[T any](sl []T, f CmpFunc[T]) []T

UniqueFunc returns the unique values from a slice using a function to compare values.

func Values added in v1.0.0

func Values[T any](sl []T) iter.Seq[T]

Values returns an iterator over the values in the slice.

Types

type CmpFunc

type CmpFunc[T any] func(v1, v2 T) bool

A CmpFunc compares two values and returns true if they are equal.

type Float

type Float interface {
	~float32 | ~float64
}

type LessFunc

type LessFunc[T any] func(v1, v2 T) bool

A LessFunc compares two values and returns true if the first is less then the second.

type Map2Func added in v0.2.0

type Map2Func[T, U, V any] func(x T, y U) V

A Map2Func maps two values to another value.

type Map3Func added in v0.2.0

type Map3Func[T, U, V, W any] func(x T, y U, z V) W

A Map3Func maps three values to another value.

type MapFunc

type MapFunc[T, U any] func(x T) U

A MapFunc maps a value to another value which may have a different type.

type Number

type Number interface {
	Float | Signed | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered

type Ordered interface {
	Number | ~string
}

type Predicate

type Predicate[T any] func(v T) bool

Predicate function type

type ReduceFunc added in v0.2.0

type ReduceFunc[T, U any] func(acc U, v T) U

Reduce function

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Jump to

Keyboard shortcuts

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