Documentation ¶
Overview ¶
Package slices is a utility package for working with slices, just like the strings and bytes packages.
Index ¶
- func Clone[T any](s []T) []T
- func Contains[T constraints.Ordered](s []T, el T) bool
- func Count[T constraints.Ordered](s []T, el T) int
- func Equal[T constraints.Ordered](a, b []T) bool
- func Index[T constraints.Ordered](s []T, el T) int
- func Join[T any](lhs, rhs []T) []T
- func Last[T any](s []T) T
- func LastIndex[T constraints.Ordered](s []T, el T) int
- func Map[T any](mapping func(T) T, s []T) []T
- func Prod[T constraints.Integer | constraints.Float](s []T) T
- func Repeat[T any](s []T, count int) []T
- func Replace[T constraints.Ordered](s []T, old, new T, n int) []T
- func ReplaceAll[T constraints.Ordered](s []T, old, new T) []T
- func Reverse[T any](s []T) []T
- func Sum[T constraints.Integer | constraints.Float](s []T) T
- func WithCap[T any](c int) []T
- func WithLen[T any](l int) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶ added in v1.0.5
func Clone[T any](s []T) []T
Clone makes a new slice and copies the given slice's elements into it.
func Contains ¶ added in v1.1.0
func Contains[T constraints.Ordered](s []T, el T) bool
Contains returns whether or not a slice contains a certain element.
func Count ¶ added in v1.1.0
func Count[T constraints.Ordered](s []T, el T) int
Count counts the number of instances of el in s
func Equal ¶
func Equal[T constraints.Ordered](a, b []T) bool
Equal returns whether or not two slices are equal.
func Index ¶ added in v1.1.0
func Index[T constraints.Ordered](s []T, el T) int
Index returns the index of the first instance of el in s, or -1 if el is not present in s.
func Join ¶ added in v1.1.0
func Join[T any](lhs, rhs []T) []T
Join joins two slices together. If an argument is nil, it returns a clone of the other argument. If both are nil, it returns nil.
func Last ¶ added in v1.1.0
func Last[T any](s []T) T
Last returns the last element in a slice. Panics if s is empty.
func LastIndex ¶ added in v1.1.0
func LastIndex[T constraints.Ordered](s []T, el T) int
LastIndex returns the index of the last instance of el in s, or -1 if el is not present in s.
func Map ¶ added in v1.1.0
func Map[T any](mapping func(T) T, s []T) []T
Map returns a copy of the slice s with all its elements modified according to the mapping function.
func Prod ¶
func Prod[T constraints.Integer | constraints.Float](s []T) T
Prod returns the product of the elements of the slice.
func Repeat ¶ added in v1.1.0
Repeat returns a new slice consisting of count copies of the slice s. It panics if count is negative or if the result of (len(s) * count) overflows.
func Replace ¶ added in v1.1.0
func Replace[T constraints.Ordered](s []T, old, new T, n int) []T
Replace returns a copy of the slice s with the first n instances of old replaced by new. If n < 0, there is no limit on the number of replacements.
func ReplaceAll ¶ added in v1.1.0
func ReplaceAll[T constraints.Ordered](s []T, old, new T) []T
ReplaceAll returns a copy of the slice s with all instances of old replaced by new.
func Reverse ¶ added in v1.1.0
func Reverse[T any](s []T) []T
Reverse returns a copy of the slice s with order of the elements reversed.
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](s []T) T
Sum returns the sum of the elements of the slice.
Types ¶
This section is empty.