sliceutil

package
v2.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package sliceutil provides functions for working with slices.

The "set" helpers are simple implementations, and don't operate on true "sets" (e.g. it will retain order, []int64 can contain duplicates). Consider using something like golang-set if you want to use sets and care a lot about performance: https://github.com/deckarep/golang-set

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSVtoInt64Slice

func CSVtoInt64Slice(csv string) ([]int64, error)

CSVtoInt64Slice converts a string of integers to a slice of int64.

func Choose

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

Choose chooses a random item from the list.

func ChooseString deprecated added in v2.2.1

func ChooseString(l []string) string

ChooseString see Choose

Deprecated: use Choose

func Complement

func Complement[T comparable](a, b []T) (aOnly, bOnly []T)

Complement returns the complement of the two lists; that is, the first return value will contain elements that are only in "a" (and not in "b"), and the second return value will contain elements that are only in "b" (and not in "a").

func Contains

func Contains[T comparable](tt []T, item T) bool

Contains returns true if item is in the provided slice.

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 Filter

func Filter[T comparable](tt []T, fn func(T) bool) []T

Filter filters a list. The function will be called for every item and those that return false will not be included in the returned list.

func FilterEmpty

func FilterEmpty[T comparable](t T) bool

FilterEmpty can be used as an argument for Filter() and will return false if e is zero value.

func FilterInt deprecated added in v2.2.1

func FilterInt(list []int64, fun func(int64) bool) []int64

FilterInt see Filter

Deprecated: use Filter

func FilterString deprecated added in v2.2.1

func FilterString(list []string, fun func(string) bool) []string

FilterString see Filter

Deprecated: use Filter

func FilterStringEmpty deprecated added in v2.2.1

func FilterStringEmpty(e string) bool

FilterStringEmpty see FilterEmpty

Deprecated: use FilterEmpty

func InFoldedStringSlice

func InFoldedStringSlice(list []string, str string) bool

InFoldedStringSlice reports whether str is within list(case-insensitive)

func InInt64Slice deprecated added in v2.2.1

func InInt64Slice(list []int64, i int64) bool

InInt64Slice see Contains

Deprecated: use Contains

func InIntSlice deprecated added in v2.2.1

func InIntSlice(list []int, i int) bool

InIntSlice see Contains

Deprecated: use Contains

func InStringSlice deprecated added in v2.2.1

func InStringSlice(list []string, str string) bool

InStringSlice see Contains

Deprecated: use Contains

func InterfaceSliceTo

func InterfaceSliceTo(src []interface{}, dst interface{}) interface{}

InterfaceSliceTo converts []interface to any given slice. It will ~optimistically~ try to convert interface item to the dst item type

func Intersection

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

Intersection returns the elements common to both a and b

func Join

func Join[T any](tt []T) string

Join converts a slice of T to a comma separated string. Useful for inserting into a query without the option of parameterization.

func JoinInt deprecated added in v2.2.1

func JoinInt(ints []int64) string

JoinInt see Join

Deprecated: use Join

func JoinWith added in v2.1.1

func JoinWith[T any](tt []T, delim string) string

JoinWith converts a slice of T to a string using the provided delim.

func Map

func Map[T comparable](tt []T, fn func(T) T) []T

Map returns a list where each item in list has been modified by fn

func MergeUnique

func MergeUnique[T comparable](tt [][]T) (result []T)

MergeUnique takes a slice of slices and returns an unsorted slice of unique entries.

func Range

func Range(start, end int) []int

Range creates an []int counting at "start" up to (and including) "end".

func Remove

func Remove[T comparable](tt []T, remove T) (out []T)

Remove removes any occurrence of a string from a slice.

func RemoveString deprecated added in v2.2.1

func RemoveString(list []string, s string) (out []string)

RemoveString see Remove

Deprecated: use Remove

func Repeat

func Repeat[T any](t T, n int) (tt []T)

Repeat returns a slice with the item t reated n times.

func RepeatString deprecated added in v2.2.1

func RepeatString(s string, n int) (r []string)

RepeatString see Repeat

Deprecated: use Repeat

func StringMap deprecated added in v2.2.1

func StringMap(list []string, f func(string) string) []string

StringMap see Map

Deprecated: use Map

func ToAnySlice added in v2.2.3

func ToAnySlice[T any](tt []T) []any

ToAnySlice converts a slice of T to a slice of any.

func UniqInt64 deprecated added in v2.2.1

func UniqInt64(list []int64) []int64

UniqInt64 see Unique

Deprecated: use Unique

func UniqString deprecated added in v2.2.1

func UniqString(list []string) []string

UniqString see Unique

Deprecated: use Unique

func Unique

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

Unique removes duplicate entries from a list. The list does not have to be sorted.

func UniqueMergeSlices deprecated added in v2.2.1

func UniqueMergeSlices(s [][]int64) (result []int64)

UniqueMergeSlices see MergeUnique

Deprecated: use MergeUnique

func Values added in v2.2.1

func Values[T comparable, N any](tt []T, fn func(T) N) []N

Values returns a list of items extracted from T, see test file for example

Types

This section is empty.

Jump to

Keyboard shortcuts

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