slice

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package slice provides functions for manipulating slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk added in v0.5.0

func Chunk[S ~[]E, E any](slice S, size int) []S

Chunk returns a new slice of slices where each slice contains at most the given size number of elements from the original slice.

func Clone added in v0.6.0

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

Clone returns a copy of the input slice.

func Concat added in v0.5.0

func Concat[S ~[]E, E any](ss ...S) S

Concat returns a new slice containing all the elements of the input slices in order.

func Count added in v0.5.0

func Count[S ~[]E, E any](s S, f func(E) bool) int

Count returns the number of elements in the slice that satisfy the given function.

func Dedup

func Dedup[S ~[]E, E comparable](s S) S

Dedup returns a new slice containing only the unique elements of the input slice.

func Equals

func Equals[S ~[]E, E comparable](a, b S) bool

Equals returns true if the two slices contain the same elements in the same order.

func EqualsAnyOrder

func EqualsAnyOrder[S ~[]E, E comparable](a, b S) bool

EqualsAnyOrder returns true if the two slices contain the same elements, regardless of order.

func Every

func Every[S ~[]E, E any](s S, f func(E) bool) bool

Every returns true if the given function returns true for every element in the slice.

func Expand2D added in v0.4.0

func Expand2D[SS ~[]S, S ~[]E, E any](ss SS, m, n int) SS

Expand2D expands a 2D slice ss to m rows and n columns, adding elements to any short rows and appending any short columns.

func Fill

func Fill[S ~[]E, E any](s S, v E)

Fill fills the entire slice with a given value.

func FillRange

func FillRange[S ~[]E, E any](s S, v E, start, end int)

FillRange fills a range [start:end) of the slice with a given value.

func Filter

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

Filter returns a new slice containing only the elements from the original slice that satisfy the given function.

func FilterMap added in v0.5.0

func FilterMap[S ~[]E, E, R any](s S, filterFunc func(E) bool, mapFunc func(E) R) []R

FilterMap returns a new slice containing the elements of the input slice that satisfy filterFunc, transformed by mapFunc.

func Find

func Find[S ~[]E, E any](s S, f func(E) bool) (E, bool)

Find returns the first element in the slice that satisfies the given function, along with a boolean indicating whether such an element was found.

func FindDefault

func FindDefault[S ~[]E, E any](s S, f func(E) bool, defaultValue E) E

FindDefault returns the first element in s that satisfies the predicate f. If no such element is found, it returns defaultValue.

func FindIndex

func FindIndex[S ~[]E, E any](s S, f func(E) bool) int

FindIndex returns the index of the first element in the slice that satisfies the given function, or -1 if no such element was found.

func FindLast

func FindLast[S ~[]E, E any](s S, f func(E) bool) (E, bool)

FindLast returns the last element in the slice that satisfies the given function, along with a boolean indicating whether such an element was found.

func FindLastDefault

func FindLastDefault[S ~[]E, E any](s S, f func(E) bool, defaultValue E) E

FindLastDefault returns the last element in s that satisfies the predicate f. If no such element is found, it returns defaultValue.

func FindLastIndex

func FindLastIndex[S ~[]E, E any](s S, f func(E) bool) int

FindLastIndex returns the index of the last element in the slice that satisfies the given function, or -1 if no such element was found.

func Flat

func Flat[SS ~[]S, S ~[]E, E any](s SS) S

Flat flattens a slice of slices into a single slice.

func ForEach

func ForEach[S ~[]E, E any](s S, f func(E))

ForEach applies the given function to every element in the slice.

func ForEachIndex

func ForEachIndex[S ~[]E, E any](s S, f func(v E, i int))

ForEachIndex applies the given function to every element in the slice along with its index.

func Group

func Group[S ~[]E, E any, K comparable](s S, f func(E) K) map[K]S

Group groups the elements of a slice by applying a given function to each element and using the result as a key in a map.

func GroupMap added in v0.5.0

func GroupMap[S ~[]E, E any, K comparable, V any](s S, keyFunc func(E) K, mapFunc func(E) V) map[K][]V

GroupMap groups the elements of a slice by applying a key function and a map function to each element, using the result of the key function as a key in a map and the result of the map function as a value in a slice.

func Includes

func Includes[S ~[]E, E comparable](s S, v E) bool

Includes returns true if the given value is found in the slice, otherwise false.

func IndexOf

func IndexOf[S ~[]E, E comparable](s S, v E) int

IndexOf returns the index of the first occurrence of the given value in the slice, or -1 if not found.

func IndexOfFrom

func IndexOfFrom[S ~[]E, E comparable](s S, v E, from int) int

IndexOfFrom returns the index of the first occurrence of the given value in the slice, starting from the given index, or -1 if not found.

func Insert added in v0.3.0

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

Insert inserts the element v into slice s at the given index i.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns a slice of keys extracted from the input map m. The keys are returned in no particular order. It returns nil if the map is empty.

func LastIndexOf

func LastIndexOf[S ~[]E, E comparable](s S, v E) int

LastIndexOf returns the index of the last occurrence of the given value in the slice, or -1 if not found.

func Make2D added in v0.4.0

func Make2D[T any](m, n int) [][]T

Make2D returns a new 2D slice with m rows and n columns.

func Map

func Map[S ~[]E, E, R any](s S, f func(E) R) []R

Map applies the given function to every element in the slice and returns a new slice containing the results.

func Most

func Most[S ~[]E, E any](s S, f func(v, most E) bool) E

Most returns the element in the slice that satisfies the given function and is "most" according to that function. The definition of "most" is left up to the caller of this function.

func Move added in v0.3.0

func Move[S ~[]E, E any](s S, a, b int)

Move moves the element at index a to index b in slice s.

func NoNil added in v0.3.0

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

NoNil returns the input slice with a non-nil value. If the input slice is nil, it returns a new empty slice of the same type.

func Pop

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

Pop removes and returns the last element of a slice, along with the remaining slice. If the slice is empty, Pop returns a zero value and a nil slice.

func Push

func Push[T any](s []T, v T) []T

Push adds an element to the end of a slice and returns the resulting slice.

func Random

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

Random returns a random element from a slice and a new slice with the randomly selected element removed. If the input slice is empty, it returns a zero value and a nil slice.

func Range added in v0.3.0

func Range[T constraints.Integer](start, end T) []T

Range returns a slice of integers from start (inclusive) to end (exclusive). If start is greater than or equal to end, an empty slice is returned.

func Reduce

func Reduce[S ~[]E, E any, R any](s S, f func(v E, acc R) R, init R) R

Reduce applies a function to each element of a slice, accumulating the result into an initial value.

func ReduceRight

func ReduceRight[S ~[]E, E, R any](s S, f func(v E, acc R) R, init R) R

ReduceRight applies a function to each element of a slice in reverse order, accumulating the result into an initial value.

func Remove added in v0.3.0

func Remove[S ~[]E, E comparable](s S, v E) S

Remove returns a new slice with the first occurrence of v removed from s. If v is not found in s, Remove returns s unchanged.

func RemoveFunc added in v0.3.0

func RemoveFunc[S ~[]E, E any](s S, f func(E) bool) S

RemoveFunc returns a new slice with the first element e in s for which f(e) is true removed. If no such element is found, RemoveFunc returns s unchanged.

func RemoveIndex added in v0.3.0

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

RemoveIndex returns a new slice with the element at index i removed from s. If i is out of bounds for s, RemoveIndex returns s unchanged.

func Reverse

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

Reverse reverses the elements of a slice in place.

func ReverseCopy

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

ReverseCopy returns a new slice with the elements in reverse order.

func Shift

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

Shift removes and returns the first element of a slice, along with the remaining slice. If the slice is empty, Shift returns a zero value and a nil slice.

func Shuffle added in v0.3.0

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

Shuffle randomizes the order of elements in the given slice using rand.Shuffle. Note that the function modifies the original slice, and does not return a new one.

func Some

func Some[S ~[]E, E any](s S, f func(E) bool) bool

Some returns true if at least one element in the slice satisfies a predicate function.

func Sort added in v0.2.0

func Sort[S ~[]E, E any](s S, less func(a, b E) bool)

Sort sorts the elements of slice s in increasing order, according to the order defined by the less function. The less function returns true if the first element should be ordered before the second element.

func Sum

func Sum[S ~[]E, E Number](s S) E

Sum returns the sum of all elements in a slice of type T.

func Unshift

func Unshift[T any](s []T, v T) []T

Unshift adds an element to the beginning of a slice and returns the resulting slice.

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns a slice of values extracted from the input map m. The values are returned in no particular order. It returns nil if the map is empty.

Types

type Number

type Number interface {
	constraints.Complex | constraints.Float | constraints.Integer
}

Number is a type constraint that allows only complex, float, and integer types.

type Zipped added in v0.5.0

type Zipped[A, B any] struct {
	A A
	B B
}

func Zip added in v0.5.0

func Zip[SA ~[]A, SB ~[]B, A, B any](sa SA, sb SB) []Zipped[A, B]

Zip returns a new slice of pairs where the i-th pair contains the i-th elements of each of the input slices. If the input slices have different lengths, the resulting slice will have length equal to the length of the shortest input slice.

Jump to

Keyboard shortcuts

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