zslice

package
v0.0.0-...-10baa64 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 8 Imported by: 6

Documentation

Overview

Package zslice implements generic functions for slices.

Index

Constants

This section is empty.

Variables

This section is empty.

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 cmp.Ordered](list []T) bool

IsUniq reports if the list contains unique values.

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 cmp.Ordered](list []T) T

Max gets the highest value from a list.

func Min

func Min[T cmp.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 cmp.Ordered](a, b []T) bool

SameElements reports if the two slices have the same elements.

This is similar to slices.Equal, but don'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 cmp.Ordered](list []T) []T

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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