slutil

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Slice utility pkg.

Index

Constants

This section is empty.

Variables

View Source
var (
	FilterEmptyStr = FilterEmptyStrFunc
)

Functions

func Concat added in v0.3.8

func Concat[T any](a []T, b ...[]T) []T

func Copy added in v0.3.10

func Copy[T any](v []T) []T

func CopyFilter

func CopyFilter[T any](l []T, f func(T) (incl bool)) []T

Filter slice value.

The original slice is not modified only copied.

func CopyFilterUpdate added in v0.4.11

func CopyFilterUpdate[T any](l []T, filterUpdate func(T) (t T, incl bool)) []T

func Distinct

func Distinct(l []string) []string

Filter duplicate values

func DistinctFunc

func DistinctFunc() func([]string) []string

Filter duplicate values

func FastDistinct

func FastDistinct(l []string) []string

Filter duplicate values, faster but values are sorted, and the slice values are filtered in place.

func Filter

func Filter[T any](l []T, f func(T) (incl bool)) []T

Filter slice values in place.

Be cautious that both slices are backed by the same array.

func FilterEmptyStrFunc added in v0.3.0

func FilterEmptyStrFunc() func([]string) []string

func FilterFunc

func FilterFunc[T any](f func(T) bool) func([]T) []T

Filter slice value.

The original slice is not modified only copied.

func FilterIdx added in v0.3.10

func FilterIdx[T any](l []T, f func(int, T) (incl bool)) []T

Filter slice values in place.

Be cautious that both slices are backed by the same array.

func First added in v0.4.0

func First[T any](v []T) (t T, ok bool)

func FirstMatch added in v0.4.0

func FirstMatch[T any](items []T, f func(T) bool) (T, bool)

Select one from the slice that matches the condition.

func Flatten added in v0.4.6

func Flatten[T any](t [][]T) []T

func MapFunc

func MapFunc[T any, V any](mapFunc func(t T) V) func([]T) []V

Map slice item to another.

func MapTo

func MapTo[T any, V any](ts []T, mapFunc func(t T) V) []V

Map slice item to another.

func MapToErr added in v0.4.10

func MapToErr[T any, V any](ts []T, mapFunc func(t T) (V, error)) ([]V, error)

Map slice item to another.

func MapToIdx added in v0.4.8

func MapToIdx[T any, V any](ts []T, mapFunc func(i int, t T) V) []V

Map slice item to another.

func MatchAny added in v0.4.8

func MatchAny[T any](l []T, filter func(v T) (incl bool)) (t T, ok bool)

func MergeMap

func MergeMap[K comparable, V any](vs []V, keyFunc func(v V) K) map[K]V

Merge slice of items to a map.

func MergeMapAs

func MergeMapAs[T any, K comparable, V any](ts []T, keyFunc func(t T) K, valueFunc func(t T) V) map[K]V

Merge slice of items to a map.

func MergeMapKV added in v0.4.9

func MergeMapKV[T interface{ MapKV() (K, V) }, K comparable, V any](ts []T) map[K]V

func MergeMapKVAny added in v0.4.11

func MergeMapKVAny[T interface{ MapKV() (K, V) }, K comparable, V any](ts []T) map[K]any

func MergeMapSlice

func MergeMapSlice[K comparable, V any](vs []V, keyFunc func(v V) K) map[K][]V

Merge slice of items to a map.

func MergeVarargs added in v0.3.4

func MergeVarargs[T any](fst T, args ...T) []T

func PadTable deprecated added in v0.4.6

func PadTable(table [][]string, colCnt int)

Deprecated: use tableutil.PadTable instead.

func Prepend added in v0.3.4

func Prepend[T any](v []T, ts ...T) []T

func QuoteStrSlice

func QuoteStrSlice(sl []string) []string

func RandOne added in v0.4.0

func RandOne[T any](items []*T) *T

Select random one from the slice

func Remove added in v0.4.0

func Remove[T any](v []T, idx ...int) []T

func Sort added in v0.4.10

func Sort[T ltsType](l []T)

func SortLess added in v0.4.10

func SortLess[T any, V ltsType](l []T, f func(T) V, i, j int) bool

func SortLessRev added in v0.4.10

func SortLessRev[T any, V ltsType](l []T, f func(T) V, i, j int) bool

func SortMapped added in v0.4.10

func SortMapped[T any, V ltsType](l []T, f func(T) V)

func SortMappedRev added in v0.4.10

func SortMappedRev[T any, V ltsType](l []T, f func(T) V)

func SortRev added in v0.4.10

func SortRev[T ltsType](l []T)

func SplitSubSlices

func SplitSubSlices[T any](sl []T, limit int, f func(sub []T) error) error

func TableColAt deprecated added in v0.4.0

func TableColAt(table [][]string, idx int) []string

Collect table col at idx.

Deprecated: use tableutil.TableColAt instead.

func Transform

func Transform[T any, V any](sl []T, transform func([]T) []V, updates ...func([]V) []V) []V

Transform value to another type then perform optional value updates.

See FilterFunc, MapFunc, FilterEmptyStrFunc, DistinctFunc.

func Update

func Update[T any](sl []T, fs ...func([]T) []T) []T

Update slice values.

See FilterFunc, FilterEmptyStrFunc, DistinctFunc.

func UpdateSliceValue

func UpdateSliceValue[T any](s []T, upd func(t T) T)

func UpdateTransform added in v0.3.0

func UpdateTransform[T any, V any](sl []T, transform func([]T) []V, updates ...func([]T) []T) []V

Update slice values then transform value to another type.

See FilterFunc, MapFunc, FilterEmptyStrFunc, DistinctFunc.

func VarArgAny added in v0.3.1

func VarArgAny[T any](v []T, defVal func() T) (t T)

Types

type SyncSlice added in v0.4.3

type SyncSlice[T any] struct {
	// contains filtered or unexported fields
}

func NewSyncSlice

func NewSyncSlice[T any](initCap int) *SyncSlice[T]

func (*SyncSlice[T]) Append added in v0.4.3

func (s *SyncSlice[T]) Append(t ...T)

func (*SyncSlice[T]) Clear added in v0.4.3

func (s *SyncSlice[T]) Clear()

func (*SyncSlice[T]) Copy added in v0.4.3

func (s *SyncSlice[T]) Copy() []T

func (*SyncSlice[T]) ForEach added in v0.4.3

func (s *SyncSlice[T]) ForEach(f func(t T) (stop bool))

func (*SyncSlice[T]) ForEachErr added in v0.4.3

func (s *SyncSlice[T]) ForEachErr(f func(t T) (stop bool, err error)) error

func (*SyncSlice[T]) Size added in v0.4.8

func (s *SyncSlice[T]) Size() int

Jump to

Keyboard shortcuts

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