package sliceutils
import "git.mstar.dev/mstar/goutils/sliceutils"
Package sliceutils contains various generic functions for applying an operation across an entire slice
Index ¶
- func Compact[T any](a []T, compactor func(acc T, next T) T) T
- func CompareOrdered[T comparable](a, b []T) bool
- func CompareUnordered[T comparable](a, b []T) bool
- func Contains[T comparable](a []T, b T) bool
- func ContainsFunc[T any](a []T, f func(t T) bool) bool
- func Filter[T any](arr []T, filter func(T) bool) []T
- func FromChannel[T any](c <-chan T, expectedSize uint) []T
- func Map[T any, M any](arr []T, apply func(T) M) []M
- func RemoveDuplicate[T comparable](sliceList []T) []T
- func Reverse[E any](s []E)
- func ToChannel[T any](a []T) <-chan T
Functions ¶
func Compact ¶
func Compact[T any](a []T, compactor func(acc T, next T) T) T
Compact the slice a using the compactor function. For the first call, the accumulator argument will be the default value
func CompareOrdered ¶
func CompareOrdered[T comparable](a, b []T) bool
CompareOrdered compares two slices for both element equality and element order.
func CompareUnordered ¶
func CompareUnordered[T comparable](a, b []T) bool
CompareUnorderedS compares two slices for element equality. The order of those elements does not matter.
func Contains ¶
func Contains[T comparable](a []T, b T) bool
Returns whether b exists inside a using a simple == comparison
func ContainsFunc ¶
Returns whether the given function f returns true for any element in a
func Filter ¶
Filter filters a slice using a given function. If the filter function returns true, the element stays, otherwise it gets removed.
func FromChannel ¶
FromChannel reads from a channel until closed, appending every element to a slice. If you do not know how many elements to expect, use an expectedSize of 0
func Map ¶
Map applies a given function to every element of a slice. The return type may be different from the initial type of the slice.
func RemoveDuplicate ¶
func RemoveDuplicate[T comparable](sliceList []T) []T
RemoveDuplicate removes all duplicates inside a slice.
func Reverse ¶
func Reverse[E any](s []E)
Reverse reverses a given slice.
func ToChannel ¶
func ToChannel[T any](a []T) <-chan T
Returns a channel that all elements in a will be written to in order. Once all values of a have been sent, the channel will be closed. The channel must be fully consumed until closed. Otherwise a goroutine will be leaked
Source Files ¶
sliceUtils.go
- Version
- v1.16.1 (latest)
- Published
- Jun 17, 2025
- Platform
- linux/amd64
- Last checked
- 1 week ago –
Tools for package owners.