package sliceutil

import "github.com/teamwork/utils/sliceutil"

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

Functions

func CSVtoInt64Slice

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

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

func ChooseString

func ChooseString(l []string) string

ChooseString chooses a random item from the list.

func Complement

func Complement(a, b []int64) (aOnly, bOnly []int64)

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 Difference

func Difference(set []int64, others ...[]int64) []int64

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

func FilterInt

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

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

func FilterIntEmpty

func FilterIntEmpty(e int64) bool

FilterIntEmpty can be used as an argument for FilterInt() and will return false if e is empty or contains only whitespace.

func FilterString

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

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

func FilterStringEmpty

func FilterStringEmpty(e string) bool

FilterStringEmpty can be used as an argument for FilterString() and will return false if e is empty or contains only whitespace.

func InFoldedStringSlice

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

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

func InInt64Slice

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

InInt64Slice reports whether i is within list

func InIntSlice

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

InIntSlice reports whether i is within list

func InStringSlice

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

InStringSlice reports whether str is within list(case-sensitive)

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(a, b []int64) []int64

Intersection returns the elements common to both a and b

func JoinInt

func JoinInt(ints []int64) string

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

func Range

func Range(start, end int) []int

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

func RemoveString

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

RemoveString removes any occurrence of a string from a slice.

func RepeatString

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

RepeatString returns a slice with the string s reated n times.

func StringMap

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

StringMap returns a list strings where each item in list has been modified by f

func UniqInt64

func UniqInt64(list []int64) []int64

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

func UniqString

func UniqString(list []string) []string

UniqString removes duplicate entries from list.

func UniqueMergeSlices

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

UniqueMergeSlices takes a slice of slices of int64s and returns an unsorted slice of unique int64s.

Source Files

sets.go sliceutil.go

Version
v1.0.0 (latest)
Published
Mar 14, 2022
Platform
linux/amd64
Imports
6 packages
Last checked
20 hours ago

Tools for package owners.