zstdzgo.at/zstd/zslice Index | Files

package zslice

import "zgo.at/zstd/zslice"

Package zslice implements generic functions for slices.

Index

Functions

func AppendCopy

func AppendCopy[T any](s []T, app T, more ...T) []T

AppendCopy is like append(), but ensures the new value is always a copy.

The len and cap will always be set to exactly the len and cap of the new array.

func Choose

func Choose[T any](list []T) T

Choose a random item from the list.

func ContainsAny

func ContainsAny[T comparable](list []T, find ...T) bool

ContainsAny reports whether any of the strings are in the list

func Copy

func Copy[T any](src []T, len, cap int) []T

Copy a slice.

This is like slices.Clone, but you can set a len and cap for the new slice; this can be larger than the src slice to prevent copying the array if you're appending more items later, or lower if you want to copy and truncate the array.

Like make(), this will panic if len > cap.

func Difference

func Difference[T comparable](set []T, others ...[]T) []T

Difference returns a new slice with elements that are in "set" but not in "others".

func Intersect

func Intersect[T comparable](a, b []T) []T

Intersect returns a new slice with elements that are in both "a" and "b".

func IsUniq

func IsUniq[T ordered](list []T) bool

IsUniq reports if the list contains unique values.

func LastIndex

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

LastIndex returns the index of the last occurrence of v in s, or -1 if not present.

func Longest

func Longest(list []string) int

Longest gets the longest string value in this list.

func LongestFunc

func LongestFunc[T any](list []T, f func(T) string) int

LongestFunc gets the longest string value in this list.

for example to get the longest email address from a []mail.Address:

zslice.LongestFunc(addrs, func(s mail.Address) string { return s.Address })

func Max

func Max[T ordered](list []T) T

Max gets the highest value from a list.

func Min

func Min[T ordered](list []T) T

Min gets the lowest value from a list.

func Remove

func Remove[T comparable](l *[]T, name T) bool

Remove all values from a list.

The return value indicates if this value was found at all.

func RemoveIndexes

func RemoveIndexes[T any](l *[]T, indexes ...int)

RemoveIndexes removes all the given indexes.

The indexes is expected to be sorted from lowest to highest.

Will panic on out of bounds.

func Repeat

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

Repeat returns a slice with the value v repeated n times.

func SameElements

func SameElements[T ordered](a, b []T) bool

SameElements reports if the two slices have the same elements.

This is similar to slices.Equal, but doesn't take order in to account.

func Shuffle

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

Shuffle randomizes the order of values.

This uses math/rand, and is not "true random".

func Uniq

func Uniq[T comparable](list []T) []T

Uniq removes duplicate entry from the list.

The order will be preserved, and the first item will be kept. This is slower than UniqSort().

func UniqSort

func UniqSort[T ordered](list []T) []T

UniqSort removes duplicate entries from list; the list will be sorted.

Source Files

zslice.go

Version
v0.0.0-20240930202209-a63c3335042a (latest)
Published
Sep 30, 2024
Platform
linux/amd64
Imports
7 packages
Last checked
2 days ago

Tools for package owners.